
###

Cleaned replication files for "It's the Economy: The Effect of Economic Policy Appeals on Latino Independents"

Author: Derek Wakefield (djwakef@emory.edu)

Journal: Political Behavior


```{r Library load}

## Standard packages
require(tidyverse)
require(readxl)
require(ggplot2)
require(ltm)

## Regressions
require(sandwich)
require(lmtest)
require(miceadds)
require(lfe)
require(modelsummary)

## Graphs and tables
require(ggsignif)
require(ggeffects)
require(data.table)
require(htmltools)
require(webshot)
require(viridis)
require(stargazer)
require(sjPlot)
```


#### Data load and clean

```{r Study 1 Data clean}

study1.uncleaned.df <- read_csv("njev_study1_unclean.csv")


study1.cleaned.df <- study1.uncleaned.df %>%
  filter(StartDate > "2021-04-06 12:00:00",
         Complete == 1)

## Cleaning outcomes
## Lowest number is lowest response (start at 1)
## Goes higher based on more positive response


## Diagnostics

  # study1.cleaned.df %>%
  # group_by(likelihood_vote,
  #          Treatment) %>%
  # summarize(count=n())%>%
  # print()


## Likelihood of vote

study1.cleaned.df <- study1.cleaned.df %>%
  mutate(turnout.5pt = ifelse(
    likelihood_vote=="Extremely likely",
    5,
  ifelse(
    likelihood_vote=="Likely",
    4,
  ifelse(
    likelihood_vote=="Somewhat likely",
    3,
  ifelse(
    likelihood_vote=="Unlikely",
    2,
  ifelse(
    likelihood_vote=="Extremely unlikely",
    1,
  ifelse(
    (is.na(likelihood_vote)==TRUE),
    0,
    0
    )))))
  )
)


## Fisher Vote

study1.cleaned.df <- study1.cleaned.df %>%
  mutate(fishervote.5pt = ifelse(
    fisher_vote=="Extremely likely",
    5,
  ifelse(
    fisher_vote=="Likely",
    4,
  ifelse(
    fisher_vote=="Somewhat likely",
    3,
  ifelse(
    fisher_vote=="Unlikely",
    2,
  ifelse(
    fisher_vote=="Extremely unlikely",
    1,
  ifelse(
    (is.na(fisher_vote)==TRUE),
    0,
    0
    )))))
  )
)


## Thermometers

study1.cleaned.df <- study1.cleaned.df %>%
  mutate(fishertherm.100 = as.numeric(Candidate_therm_1),
         demtherm.100 = as.numeric(DemParty_therm_1))





######### Treatments

        

######### Moderators

## Party is set


## Latino Group Consciousness

# First: Latino ID Strength

study1.cleaned.df <- study1.cleaned.df %>%
  mutate(LatIdStr.5pt = ifelse(
    LatIdStr == "Extremely important",
    5,
    ifelse(LatIdStr == "Very important",
    4,
    ifelse(LatIdStr == "Somewhat important",
    3,
    ifelse(LatIdStr == "Not too important",
    2,
    ifelse(LatIdStr == "Not important at all",
    1,
    NA)
    ))))
) %>%
## Adding High/Low
mutate(LatIdStr.2pt = ifelse(
    LatIdStr == "Extremely important",
    1,
    ifelse(LatIdStr == "Very important",
    1,
    ifelse(LatIdStr == "Somewhat important",
    0,
    ifelse(LatIdStr == "Not too important",
    0,
    ifelse(LatIdStr == "Not important at all",
    0,
    NA)
  ))))
)




# Latinx Group Commonality

study1.cleaned.df <- study1.cleaned.df %>%
mutate(LatComm.4pt = ifelse(
  LatGroupCommonality == "A lot in common",
  4,
  ifelse(LatGroupCommonality == "A fair amount in common",
         3,
         ifelse(LatGroupCommonality == "Only a little in common",
                2,
                ifelse(LatGroupCommonality == "Nothing in common",
                       1,
                       0)
         )))
)


study1.cleaned.df <- study1.cleaned.df%>%
  mutate(LatComm.4pt = ifelse(
    is.na(LatGroupComm_otro)==TRUE,
    LatComm.4pt,
  ifelse( 
    LatGroupComm_otro == "A lot in common",
    4,
  ifelse(
    LatGroupComm_otro == "A fair amount in common",
    3,
  ifelse(
    LatGroupComm_otro == "Only a little in common",
    2,
  ifelse(
    LatGroupComm_otro == "Nothing in common",
    1,
    LatComm.4pt))))))




# Latinx Perceive Discrimination

study1.cleaned.df <- study1.cleaned.df %>%
  mutate(LatDiscrim.4pt = ifelse(
   LatPerceiveDiscrim == "Major problem",
    4,
    ifelse(LatPerceiveDiscrim == "Moderate problem",
    3,
    ifelse(LatPerceiveDiscrim == "Minor problem",
    2,
    ifelse(LatPerceiveDiscrim == "Not a problem",
    1,
    NA)
  )))
)


# Latinx Desire for Collective

study1.cleaned.df <- study1.cleaned.df %>%
  mutate(LatDesColl.3pt = ifelse(
    LatDesireCollective == "Better off",
    3,
    ifelse(LatDesireCollective == "Not much different",
    2,
    ifelse(LatDesireCollective == "Worse off",
    1,
    NA)
  ))
)


# Latinx linked fate

study1.cleaned.df <- study1.cleaned.df %>%
mutate(LatLinkedFate.5pt = ifelse(
  LatLinkedFate == "A great deal",
  5,
  ifelse(LatLinkedFate == "A lot",
  4,
  ifelse(LatLinkedFate == "A moderate amount",
  3,
  ifelse(LatLinkedFate == "A little",
  2,
  ifelse(LatLinkedFate == "None at all",
  1,
  NA))))
  )
)

### Latinx Group Consciousness scale

study1.cleaned.df <- study1.cleaned.df %>%
  mutate(LatGC.16pt = LatIdStr.5pt+
           LatComm.4pt+
           LatDiscrim.4pt+
           LatDesColl.3pt)


## Immigrant generation

study1.cleaned.df <- study1.cleaned.df %>%
  mutate(ImmGen =
  ifelse(Nativity == "No, not born in the U.S.",
    1,
  ifelse(ParentNativity %in% c("One parent born in the U.S.",
                               "Neither parent born in the U.S."),
    2,
  3)))


### Latinx ID/GC Diagnostic

diag<-study1.cleaned.df %>%
  group_by(ImmGen,
           LatIdStr.5pt,
           LatComm.4pt,
           LatDiscrim.4pt,
           LatDesColl.3pt,
           LatLinkedFate.5pt,
           LatGC.16pt)%>%
  summarize(count=n())%>%
  print()





## National origin


## Fixing "Other"s
## This isn't working, not sure why

# study1.cleaned.df <- study1.cleaned.df %>%
#   mutate(NatlOrigin =
#     ifelse(
#       NatlOriginOther == "Columbia",
#       "Colombia",
#     ifelse(
#       NatlOriginOther == "Dominican Republic",
#       "Dominican Repubic",
#     ifelse(
#       NatlOriginOther == "N America/ Mexico",
#       "Mexico",
#     ifelse(
#       NatlOriginOther == "Puerto Rico, Panama, England, Germany, France",
#       "Puerto Rico",
#     ifelse(
#       is.na(NatlOriginOther)==TRUE,
#       NatlOrigin,
#       NA)
#     )
#     )
#     )
#     )
#   )



## Grouping by cluster

study1.cleaned.df <- study1.cleaned.df %>%
  mutate(NatlOrigin_Grouped = ifelse(
      NatlOrigin == "Mexico",
      "Mexico",
    ifelse(NatlOrigin %in% c("Costa Rica",
                          "El Salvador",
                          "Guatemala",
                          "Honduras",
                          "Nicaragua",
                          "Panama"),
      "Central America",
    NatlOrigin)
  )
  )
    

diag <- study1.cleaned.df %>%
  group_by(NatlOrigin,
           NatlOrigin_Grouped) %>%
  summarize(count=n())%>%
  print()




## 


## Latino/Hispanic/Latinx preference

diag <- study1.cleaned.df%>%
  group_by(HispLatPref,
           HispLat_pref)%>%
  summarize(count=n())%>%
  print()

study1.cleaned.df <- subset(study1.cleaned.df,
                    select = -c(HispLat_pref))


## Gender


diag <- study1.cleaned.df %>%
  group_by(Gender)%>%
  summarize(count=n())%>%
  print()




## Undocumented social network

diag <- study1.cleaned.df%>%
  group_by(undoc_know)%>%
  summarize(count=n())%>%
  print()



## Policy views

study1.cleaned.df <- study1.cleaned.df %>%
  mutate(EconSpending = as.numeric(EconSpending_1),
           EconTaxes = as.numeric(EconTaxes_1),
           ImmigSecurity = as.numeric(ImmigSecurity_1),
           ImmigPathway = as.numeric(ImmigPathway_1),
           ImmigCrossing = as.numeric(ImmigCrossing_1),
           ImmigLegal = as.numeric(ImmigLegal_1))


## Income

study1.cleaned.df <- study1.cleaned.df %>%
  mutate(Income_char = Income) 

study1.cleaned.df<-study1.cleaned.df%>%
  mutate(Income = 
    ifelse(
      Income_char == "Under $5,000",
      5000,
    ifelse(
      Income_char == "$5,000-9,999",
      7500,
    ifelse(
      Income_char == "$10,000-12,499",
      11250,
    ifelse(
      Income_char == "$12,500-14,999",
      13750,
    ifelse(
      Income_char == "$15,000-17,499",
      16250,
    ifelse(
      Income_char == "$17,500-19,999",
      18750,
    ifelse(
      Income_char == "$20,000-22,499",
      21250,
    ifelse(
      Income_char == "$22,500-24,999",
      23750,
    ifelse(
      Income_char == "$25,000-27,499",
      26250,
    ifelse(
      Income_char == "$27,500-29,999",
      28750,
    ifelse(
      Income_char == "$30,000-34,999",
      32500,
    ifelse(
      Income_char == "$35,000-39,999",
      37500,
    ifelse(
      Income_char == "$40,000-44,999",
      42500,
    ifelse(
      Income_char == "$45,000-49,999",
      47500,
    ifelse(
      Income_char == "$50,000-54,999",
      52500,
    ifelse(
      Income_char == "$55,000-59,999",
      57500,
    ifelse(
      Income_char == "$60,000-64,999",
      62500,
    ifelse(
      Income_char == "$65,000-69,999",
      67500,
    ifelse(
      Income_char == "$70,000-74,999",
      72500,
    ifelse(
      Income_char == "$75,000-79,999",
      77500,
    ifelse(
      Income_char == "$80,000-89,999",
      85000,
    ifelse(
      Income_char == "$90,000-99,999",
      95000,
    ifelse(
      Income_char == "$100,000-109,999",
      105000,
    ifelse(
      Income_char == "$110,000-124,999",
      117500,
    ifelse(
      Income_char == "$125,000-149,999",
      137500,
    ifelse(
      Income_char == "$150,000-174,999",
      162500,
    ifelse(
      Income_char == "$175,000-249,999",
      212500,
    ifelse(
      Income_char == "$250,000 or more",
      275000,
      NA)
    ))))))))))))))))))))))))))))

diag <- study1.cleaned.df%>%
  group_by(Income,
           Income_char)%>%
  summarize(count=n())%>%
  print()




##  Age

study1.cleaned.df <- study1.cleaned.df%>%
  mutate(Age_char = Age)

study1.cleaned.df<-study1.cleaned.df%>%
  mutate(Age = as.numeric(Age_char))
  
diag <- study1.cleaned.df%>%
  group_by(Age,
           Age_char)%>%
  summarize(count=n())%>%
  print()



## Race


study1.cleaned.df <- study1.cleaned.df%>%
  mutate(Race_original = Race)



## White

study1.cleaned.df <- study1.cleaned.df%>%
  mutate(White = ifelse(
    Race_original%in%c("White, Anglo, or Caucasian",
              "American Indian or Alaska Native,Black or African American,White, Anglo, or Caucasian",
              "American Indian or Alaska Native,Black or African American,White, Anglo, or Caucasian,Other",
              "American Indian or Alaska Native,White, Anglo, or Caucasian",
              "Black or African American,White, Anglo, or Caucasian"),
    1,
    0
  ))



## Black

study1.cleaned.df <- study1.cleaned.df%>%
  mutate(Black = ifelse(
    Race_original%in%c("American Indian or Alaska Native,Black or African American",
              "American Indian or Alaska Native,Black or African American,White, Anglo, or Caucasian",
              "American Indian or Alaska Native,Black or African American,White, Anglo, or Caucasian,Other",
              "Asian or Asian American,Black or African American",
              "Black or African American,White, Anglo, or Caucasian",
              "Black or African American"),
    1,
    0
  ))



## American Indian

study1.cleaned.df <- study1.cleaned.df%>%
  mutate(AmericanIndian = ifelse(
    Race_original%in%c("American Indian or Alaska Native",
                       "American Indian or Alaska Native,Black or African American",
                       "American Indian or Alaska Native,Black or African American,White, Anglo, or Caucasian",
                       "American Indian or Alaska Native,Black or African American,White, Anglo, or Caucasian,Other",
                       "American Indian or Alaska Native,White, Anglo, or Caucasian"),
    1,
    0
  ))



## Asian American

study1.cleaned.df <- study1.cleaned.df%>%
  mutate(AsianAmerican = ifelse( Race_original %in% c("Asian or Asian American",
                       "Asian or Asian American,Black or African American"),
    1,
    0))



study1.cleaned.df <- study1.cleaned.df%>%
  mutate(Other.NationalOrigin = ifelse(
    Race_6_TEXT %in% c("Chicano",
                       "caribbean",
                       "Colombian",
                       "Cuban",
                       "Indigenous to Zacatecas",
                       "Mexican",
                       "mexican-american",
                       "Mexican American",
                       "puerto rican",
                       "Puerto Rican",
                       "PR"),
    1,
    0)
  )
    
study1.cleaned.df <- study1.cleaned.df%>%
  mutate(Other.HispanicLatino = ifelse(
    Race_6_TEXT %in% c("hispanic",
                       "Hispanic",
                       "Hispano",
                       "Latin",
                       "Latina",
                       "Latino",
                       "latino",
                       "Latino american"),
    1,
    0
  )
  )



diag <- study1.cleaned.df%>%
  group_by(Race,
           Race_6_TEXT,
           White,
           Black,
           AmericanIndian,
           AsianAmerican,
           Other.HispanicLatino,
           Other.NationalOrigin)%>%
  summarize(count=n())%>%
  print()


### State

diag<-study1.cleaned.df%>%
  group_by(State)%>%
  summarize(count=n())%>%
  arrange(desc(count))%>%
  print()




### Party


diag<-study1.cleaned.df%>%
  group_by(Party,
           DemocratLean,
           IndependentLean)%>%
  summarize(count=n())%>%
  print()


diag <- study1.cleaned.df%>%
  group_by(Party,
           Treatment)%>%
  summarize(count=n())%>%
  print()


## Ideology

study1.cleaned.df <- study1.cleaned.df%>%
  mutate(Ideology_char = Ideology,
         Ideology_original = Ideology)

study1.cleaned.df <- study1.cleaned.df%>%
  mutate(Ideology = ifelse(
      Ideology_original=="Extremely liberal",
      1,
    ifelse(
      Ideology_original=="Liberal",
      2,
    ifelse(
      Ideology_original=="Slightly liberal",
      3,
    ifelse(
      Ideology_original=="Moderate, middle of the road",
      4,
    ifelse(
      Ideology_original=="Slightly conservative",
      5,
    ifelse(
      Ideology_original=="Conservative",
      6,
    ifelse(
      Ideology_original=="Extremely conservative",
      7,
    NA
  ))))))))


diag <- study1.cleaned.df%>%
  group_by(Ideology,
           Ideology_original,
           Ideology_char)%>%
  summarize(count=n())%>%
  print()



## Political Interest


diag <- study1.cleaned.df %>%
  group_by(PolInterest)%>%
  summarize(count=n())%>%
  print()


## Vote 2020

diag <- study1.cleaned.df %>%
  group_by(Vote2020)%>%
  summarize(count=n())%>%
  print()



## Social Media

diag <- study1.cleaned.df %>%
  group_by(SocialMedia_usage)%>%
  summarize(count=n())%>%
  print()



## Other participation

diag <- study1.cleaned.df %>%
  group_by(OtherParticipation)%>%
  summarize(count=n())%>%
  print()


## Religion

diag <- study1.cleaned.df %>%
  group_by(Religion,
           ReligionOther)%>%
  summarize(count=n())%>%
  print()


## Education

diag <- study1.cleaned.df %>%
  group_by(Education)%>%
  summarize(count=n())%>%
  print()



## Speed/Attention checks

diag <- study1.cleaned.df %>%
  group_by(SpeedId,
           SpeedExperiment,
           SpeedPolicy,
           AttentionCheck_1)%>%
  summarize(count=n())%>%
  print()


## Labeling Respondent Quality

study1.cleaned.df <- study1.cleaned.df %>%
  mutate(QualityResponse = ifelse(
    AttentionCheck_1 == "100.0",
    "High",
    "Low"
  )
  )


## Manipulation checks

study1.cleaned.df <- study1.cleaned.df %>%
  mutate(ManipTarget = 
    ifelse(
      HispLatPref%in%c("Hispanic","No preference"),
      maniptarget_hispanic,
    ifelse(
      HispLatPref == "Latino",
      maniptarget_latino,
    ifelse(
      HispLatPref == "Latinx",
      maniptarget_latinx,
      NA
    ))))

study1.cleaned.df <- study1.cleaned.df %>%
  mutate(ManipRace=manip_race)


diag <- study1.cleaned.df %>%
  group_by(ManipTarget,
           Treatment,
           QualityResponse)%>%
  summarize(count=n())%>%
  print()


diag <- study1.cleaned.df %>%
  group_by(ManipRace,
           Treatment)%>%
  summarize(count=n())%>%
  print()


## Party ID strength

study1.cleaned.df <- study1.cleaned.df %>%
  mutate(pid_num = ifelse(Party == "Democrat" &
                            DemocratLean == "Strong Democrat",
                          7,
                   ifelse(Party == "Democrat" & 
                            DemocratLean == "Not a very strong Democrat",
                          6,
                   ifelse(Party == "Independent" & 
                            IndependentLean == "Closer to Democratic Party",
                          5,
                   ifelse(Party == "Independent" & 
                            IndependentLean == "Neither",
                          4,
                   ifelse(Party == "Independent" &
                            IndependentLean == "Closer to Republican Party",
                          3,
                          NA))))))


## 7/14/22
## Adding Mexican national origin dummy

study1.cleaned.df <- study1.cleaned.df %>%
  mutate(Mexican = ifelse(NatlOrigin=="Mexico",
                          1,
                          0))




study1.cleaned.df <- study1.cleaned.df %>%
  mutate(vote.dv = ifelse(fishervote.5pt==1,
                          "Extremely unlikely",
                   ifelse(fishervote.5pt==2,
                          "Unlikely",
                   ifelse(fishervote.5pt==3,
                          "Neither likely nor unlikely",
                   ifelse(fishervote.5pt==4,
                          "Likely",
                   ifelse(fishervote.5pt==5,
                          "Extremely likely",
                          NA))))))

study1.cleaned.df$vote.dv <- factor(study1.cleaned.df$vote.dv,
                                    levels = c("Extremely unlikely",
                                               "Unlikely",
                                               "Neither likely nor unlikely",
                                               "Likely",
                                               "Extremely likely"))


## Cleaning turnout outcome

study1.cleaned.df$turnout.dv <- factor(study1.cleaned.df$turnout.5pt,
                                    levels = c(1,2,3,4,5))


## Fixing Treatment variable

diag <- study1.cleaned.df %>%
  group_by(Treatment) %>%
  summarize(count=n())

study1.cleaned.df <- study1.cleaned.df %>%
  mutate(Treatment = case_when(
    Treatment == "Immig" ~ "Pro-Undoc",
    Treatment == "Econ" ~ "Progressive",
    Treatment == "Raceclass" ~ "Race-Class",
    Treatment == "Control" ~ "Control"
  ))

study1.cleaned.df$Treatment <- factor(study1.cleaned.df$Treatment,
                                    levels = c("Control",
                                               "Pro-Undoc",
                                               "Progressive",
                                               "Race-Class"))


study1.cleaned.df <- study1.cleaned.df %>%
  filter(is.na(Treatment)==FALSE)

write.csv(study1.cleaned.df,
          file = "njev_study1_clean.csv")

```


```{r Study 2 Data clean}

study2.uncleaned.df <- read_csv("adtranscripts_study2_unclean.csv")

clean.df <- study2.uncleaned.df %>%
  filter(gc=="1")


## Data semantics
## 
## dataframes: name.df
## 
## old variables often include .
##
## clean variables only use _


## ## ## Treatments

## t_2: Which ad was viewed first?
##
## Then, what messages correspond with each ad? Note the naming is for clarity on my end
## as controldem is "Ad 1" and controlrep is "Ad 6". The "first" treatment ad (above)
## is "Ad 2". 
##
## t_3
## t_4
## t_5
## t_7
## t_8
## t_9
## t_10

clean.df <- clean.df %>%
  mutate(t_2 = substr(DemOrder, 1, 1),
         t_3 = substr(DemOrder, 2, 2),
         t_4 = substr(DemOrder, 3, 3),
         t_5 = substr(DemOrder, 4, 4),
         t_7 = substr(RepOrder, 1, 1),
         t_8 = substr(RepOrder, 2, 2),
         t_9 = substr(RepOrder, 3, 3),
         t_10 = substr(RepOrder, 4, 4))

## ## ## DVs

## Making DVs integers and flipping their order
## 1 = Most Negative
## 5 = Most Positive

clean.df <- clean.df %>%
  mutate(dv_controldem = 6-as.integer(dv.controldem),
         dv_controlrep = 6-as.integer(dv.controlrep),
         dv_ad2 = 6-as.integer(dv.ad2),
         dv_ad3 = 6-as.integer(dv.ad3),
         dv_ad4 = 6-as.integer(dv.ad4),
         dv_ad5 = 6-as.integer(dv.ad5),
         dv_ad7 = 6-as.integer(dv.ad7),
         dv_ad8 = 6-as.integer(dv.ad8),
         dv_ad9 = 6-as.integer(dv.ad9),
         dv_ad10 = 6-as.integer(dv.ad10),
         dv_fisher = 6-as.integer(dv.fisher),
         dv_miller = 6-as.integer(dv.miller),
         dv_turnout = 6-as.integer(dv.turnout))

## Making new DVs
##
## dv_ad#_minuscontrol

## This DV calculates the message (treatment) minus the control (anchor)

clean.df <- clean.df %>%
  mutate(dv_ad2_minuscontrol = dv_ad2 - dv_controldem,
         dv_ad3_minuscontrol = dv_ad3 - dv_controldem,
         dv_ad4_minuscontrol = dv_ad4 - dv_controldem,
         dv_ad5_minuscontrol = dv_ad5 - dv_controldem,
         dv_ad7_minuscontrol = dv_ad7 - dv_controlrep,
         dv_ad8_minuscontrol = dv_ad8 - dv_controlrep,
         dv_ad9_minuscontrol = dv_ad9 - dv_controlrep,
         dv_ad10_minuscontrol = dv_ad10 - dv_controlrep)


## Candidate forced choice

clean.df <- clean.df %>%
  mutate(dv_forcechoice = ifelse(dv.forcechoice=="1",
                                 1,
                          ifelse(dv.forcechoice=="2",
                                 0,
                                 NA)))



## Sum of ad sentiment, by candidate

clean.df <- clean.df %>%
  mutate(fisher_adsentiment = dv_controldem+dv_ad2+dv_ad3+dv_ad4+dv_ad5,
         miller_adsentiment = dv_controlrep+dv_ad7+dv_ad8+dv_ad9+dv_ad10)



## 

## For the rest, aggregate DVs by message type, not ad order
## 
## Naming goes first initial party (D/R) and second initial message code

clean.df <- clean.df %>%
          ## First Democrat
  mutate(dv_DU = ifelse(t_2 == "U",
                        dv_ad2,
                 ifelse(t_3 == "U",
                        dv_ad3,
                 ifelse(t_4 == "U",
                        dv_ad4,
                 ifelse(t_5 == "U",
                        dv_ad5,
                        NA)))),
         dv_DL = ifelse(t_2 == "L",
                        dv_ad2,
                 ifelse(t_3 == "L",
                        dv_ad3,
                 ifelse(t_4 == "L",
                        dv_ad4,
                 ifelse(t_5 == "L",
                        dv_ad5,
                        NA)))),
         dv_DP = ifelse(t_2 == "P",
                        dv_ad2,
                 ifelse(t_3 == "P",
                        dv_ad3,
                 ifelse(t_4 == "P",
                        dv_ad4,
                 ifelse(t_5 == "P",
                        dv_ad5,
                        NA)))),
         dv_DE = ifelse(t_2 == "E",
                        dv_ad2,
                 ifelse(t_3 == "E",
                        dv_ad3,
                 ifelse(t_4 == "E",
                        dv_ad4,
                 ifelse(t_5 == "E",
                        dv_ad5,
                        NA)))),
         ## Then Republican
         dv_RU = ifelse(t_7 == "U",
                        dv_ad7,
                 ifelse(t_8 == "U",
                        dv_ad8,
                 ifelse(t_9 == "U",
                        dv_ad9,
                 ifelse(t_10 == "U",
                        dv_ad10,
                        NA)))),
         dv_RL = ifelse(t_7 == "L",
                        dv_ad7,
                 ifelse(t_8 == "L",
                        dv_ad8,
                 ifelse(t_9 == "L",
                        dv_ad9,
                 ifelse(t_10 == "L",
                        dv_ad10,
                        NA)))),
         dv_RP = ifelse(t_7 == "P",
                        dv_ad7,
                 ifelse(t_8 == "P",
                        dv_ad8,
                 ifelse(t_9 == "P",
                        dv_ad9,
                 ifelse(t_10 == "P",
                        dv_ad10,
                        NA)))),
         dv_RE = ifelse(t_7 == "E",
                        dv_ad7,
                 ifelse(t_8 == "E",
                        dv_ad8,
                 ifelse(t_9 == "E",
                        dv_ad9,
                 ifelse(t_10 == "E",
                        dv_ad10,
                        NA)))))
         

## Repeating for the "minuscontrol" variables

clean.df <- clean.df %>%
          ## First Democrat
  mutate(dv_DU_minuscontrol = ifelse(t_2 == "U",
                        dv_ad2_minuscontrol,
                 ifelse(t_3 == "U",
                        dv_ad3_minuscontrol,
                 ifelse(t_4 == "U",
                        dv_ad4_minuscontrol,
                 ifelse(t_5 == "U",
                        dv_ad5_minuscontrol,
                        NA)))),
         dv_DL_minuscontrol = ifelse(t_2 == "L",
                        dv_ad2_minuscontrol,
                 ifelse(t_3 == "L",
                        dv_ad3_minuscontrol,
                 ifelse(t_4 == "L",
                        dv_ad4_minuscontrol,
                 ifelse(t_5 == "L",
                        dv_ad5_minuscontrol,
                        NA)))),
         dv_DP_minuscontrol = ifelse(t_2 == "P",
                        dv_ad2_minuscontrol,
                 ifelse(t_3 == "P",
                        dv_ad3_minuscontrol,
                 ifelse(t_4 == "P",
                        dv_ad4_minuscontrol,
                 ifelse(t_5 == "P",
                        dv_ad5_minuscontrol,
                        NA)))),
         dv_DE_minuscontrol = ifelse(t_2 == "E",
                        dv_ad2_minuscontrol,
                 ifelse(t_3 == "E",
                        dv_ad3_minuscontrol,
                 ifelse(t_4 == "E",
                        dv_ad4_minuscontrol,
                 ifelse(t_5 == "E",
                        dv_ad5_minuscontrol,
                        NA)))),
         ## Then Republican
         dv_RU_minuscontrol = ifelse(t_7 == "U",
                        dv_ad7_minuscontrol,
                 ifelse(t_8 == "U",
                        dv_ad8_minuscontrol,
                 ifelse(t_9 == "U",
                        dv_ad9_minuscontrol,
                 ifelse(t_10 == "U",
                        dv_ad10_minuscontrol,
                        NA)))),
         dv_RL_minuscontrol = ifelse(t_7 == "L",
                        dv_ad7_minuscontrol,
                 ifelse(t_8 == "L",
                        dv_ad8_minuscontrol,
                 ifelse(t_9 == "L",
                        dv_ad9_minuscontrol,
                 ifelse(t_10 == "L",
                        dv_ad10_minuscontrol,
                        NA)))),
         dv_RP_minuscontrol = ifelse(t_7 == "P",
                        dv_ad7_minuscontrol,
                 ifelse(t_8 == "P",
                        dv_ad8_minuscontrol,
                 ifelse(t_9 == "P",
                        dv_ad9_minuscontrol,
                 ifelse(t_10 == "P",
                        dv_ad10_minuscontrol,
                        NA)))),
         dv_RE_minuscontrol = ifelse(t_7 == "E",
                        dv_ad7_minuscontrol,
                 ifelse(t_8 == "E",
                        dv_ad8_minuscontrol,
                 ifelse(t_9 == "E",
                        dv_ad9_minuscontrol,
                 ifelse(t_10 == "E",
                        dv_ad10_minuscontrol,
                        NA)))))
         



## ## ## Moderators

## Party ID

clean.df <- clean.df %>%
  rename(party_id = party.id...130)

clean.df <- clean.df%>%
  mutate(pid3 = ifelse(party_id == "Democrat",
                      "Democrat",
               ifelse(party_id == "Republican",
                      "Republican",
                      "Independent")))

## Party ID strength (7 pt)

clean.df <- clean.df %>%
  mutate(party_id_str = ifelse(party_id == "Democrat" &
                                 dem.lean == 1,
                               "Strong Democrat",
                        ifelse(party_id == "Democrat" &
                                 dem.lean == 2,
                               "Weak Democrat",
                        ifelse(party_id == "Republican" &
                                 rep.lean == 1,
                               "Strong Republican",
                        ifelse(party_id == "Republican" &
                                 rep.lean == 2,
                               "Weak Republican",
                               party_id)))))


## Numeric

clean.df <- clean.df %>%
  mutate(pid_num = ifelse(party_id_str=="Strong Democrat",
                          7,
                   ifelse(party_id_str=="Weak Democrat",
                          6,
                   ifelse(party_id_str=="Independent lean Dem",
                          5,
                   ifelse(party_id_str=="Independent no lean",
                          4,
                   ifelse(party_id_str=="Independent lean Rep",
                          3,
                   ifelse(party_id_str=="Weak Republican",
                          2,
                   ifelse(party_id_str=="Strong Republican",
                          1,
                          NA
                          ))))))))


## I want moderator factor level labels to include N = #
## So, I set the factors here


## Full sample, party ID str 

levels.df <- clean.df %>%
  group_by(party_id_str)%>%
  summarize(count=n())

levels_party_id_str <- c(paste(paste("Strong Democrat (N = ", as.character(levels.df[4,2]), sep=""),")", sep=""),
                         paste(paste("Weak Democrat (N = ", as.character(levels.df[6,2]), sep=""), ")", sep=""),
                         paste(paste("Independent lean Dem (N = ", as.character(levels.df[1,2]), sep=""),")", sep=""),
                         paste(paste("Independent no lean (N = ", as.character(levels.df[3,2]), sep=""),")", sep=""),
                         paste(paste("Independent lean Rep (N = ", as.character(levels.df[2,2]), sep=""),")", sep=""),
                         paste(paste("Weak Republican (N = ", as.character(levels.df[7,2]), sep=""),")", sep=""),
                         paste(paste("Strong Republican (N = ", as.character(levels.df[5,2]), sep=""),")", sep=""))

clean.df <- clean.df %>%
  mutate(party_id_str_old = party_id_str)

clean.df <- clean.df %>%
  mutate(party_id_str = ifelse(party_id_str_old == "Strong Democrat",
                               levels_party_id_str[1],
                        ifelse(party_id_str_old == "Weak Democrat",
                               levels_party_id_str[2],
                        ifelse(party_id_str_old == "Independent lean Dem",
                               levels_party_id_str[3],
                        ifelse(party_id_str_old == "Independent no lean",
                               levels_party_id_str[4],
                        ifelse(party_id_str_old == "Independent lean Rep",
                               levels_party_id_str[5],
                        ifelse(party_id_str_old == "Weak Republican",
                               levels_party_id_str[6],
                        ifelse(party_id_str_old == "Strong Republican",
                               levels_party_id_str[7],
                               NA
                               ))))))))

clean.df$party_id_str <- factor(clean.df$party_id_str,
                                levels = c("Strong Democrat (N = 192)",
                                           "Weak Democrat (N = 157)",
                                           "Independent lean Dem (N = 324)",
                                           "Independent no lean (N = 337)",
                                           "Independent lean Rep (N = 219)",
                                           "Weak Republican (N = 140)",
                                           "Strong Republican (N = 206)"))


## Latino Identity/ Perc Discrim/ Linked fate 

## Latino identity had to be coded differently to account for Latina

## latino.id.male = Not female
## latino.id.latina = female prefers Latina
## latino.id.othera = female prefers Hispanic, Latinx

clean.df <- clean.df %>%
  mutate(lat_id_str = ifelse(gender!="2",
                             as.integer(latino.id.latino),
                      ifelse(gender=="2" &
                               panethnic.pref == "Latino",
                             as.integer(latino.id.latina),
                      ifelse(gender=="2" &
                               panethnic.pref != "Latino",
                             as.integer(latino.id.othera),
                             NA))))

clean.df <- clean.df %>%
  mutate(lat_id_str = 5 - lat_id_str)


## Perceive discrimination

clean.df <- clean.df %>%
  mutate(lat_perc_discrim = as.integer(lat.perc.discrim))

## Linked fate

clean.df <- clean.df %>%
  mutate(lat_linked_fate = as.integer(lat.linked.fate))

clean.df <- clean.df %>%
  mutate(lat_linked_fate_numeric = 5 - lat_linked_fate)

clean.df <- clean.df %>%
  rename(lat_linked_fate_old = lat_linked_fate)

clean.df <- clean.df %>%
  mutate(lat_linked_fate = ifelse(lat_linked_fate_old == 1,
                                  "A great deal",
                           ifelse(lat_linked_fate_old == 2,
                                  "A lot",
                           ifelse(lat_linked_fate_old == 3,
                                  "A moderate amount",
                           ifelse(lat_linked_fate_old == 4,
                                  "A little",
                           ifelse(lat_linked_fate_old == 5,
                                  "None at all",
                                  NA))))))


## ## Diagnostics

clean.df %>%
  group_by(lat_id_str,
           lat_perc_discrim,
           lat_linked_fate)%>%
  summarize(count=n())


## ## ## ## Other demographics


## State 

diag<-clean.df %>%
  group_by(state) %>%
  summarize(count=n())


clean.df <- clean.df %>%
  rename(state_old = state)



## 1-25
clean.df <- clean.df %>%
  mutate(state = ifelse(state_old==1,
                        "Alabama",
                 ifelse(state_old==2,
                        "Alaska",
                 ifelse(state_old==3,
                        "Arizona",
                 ifelse(state_old==4,
                        "Arkansas",
                 ifelse(state_old==5,
                        "California",
                        ifelse(state_old==6,
                        "Colorado",
                 ifelse(state_old==7,
                        "Connecticut",
                 ifelse(state_old==8,
                        "Delaware",
                 ifelse(state_old==9,
                        "District of Columbia",
                 ifelse(state_old==10,
                        "Florida",
                        ifelse(state_old==11,
                        "Georgia",
                 ifelse(state_old==12,
                        "Hawaii",
                 ifelse(state_old==13,
                        "Idaho",
                 ifelse(state_old==14,
                        "Illinois",
                 ifelse(state_old==15,
                        "Indiana",
                        ifelse(state_old==16,
                        "Iowa",
                 ifelse(state_old==17,
                        "Kansas",
                 ifelse(state_old==18,
                        "Kentucky",
                 ifelse(state_old==19,
                        "Louisiana",
                 ifelse(state_old==20,
                        "Maine",
                        ifelse(state_old==21,
                        "Maryland",
                 ifelse(state_old==22,
                        "Massachusetts",
                 ifelse(state_old==23,
                        "Michigan",
                 ifelse(state_old==24,
                        "Minnesota",
                 ifelse(state_old==25,
                        "Mississippi",
                        NA))))))))))))))))))))))))))
                      
           
## 25-rest 


clean.df <- clean.df %>%
  mutate(state = ifelse(state_old==26,
                        "Missouri",
                 ifelse(state_old==27,
                        "Montana",
                 ifelse(state_old==28,
                        "Nebraska",
                 ifelse(state_old==29,
                        "Nevada",
                 ifelse(state_old==30,
                        "New Hampshire",
                        ifelse(state_old==31,
                        "New Jersey",
                 ifelse(state_old==32,
                        "New Mexico",
                 ifelse(state_old==33,
                        "New York",
                 ifelse(state_old==34,
                        "North Carolina",
                 ifelse(state_old==35,
                        "North Dakota",
                        ifelse(state_old==36,
                        "Ohio",
                 ifelse(state_old==37,
                        "Oklahoma",
                 ifelse(state_old==38,
                        "Oregon",
                 ifelse(state_old==39,
                        "Pennsylvania",
                 ifelse(state_old==40,
                        "Puerto Rico",
                        ifelse(state_old==41,
                        "Rhode Island",
                 ifelse(state_old==42,
                        "South Carolina",
                 ifelse(state_old==43,
                        "South Dakota",
                 ifelse(state_old==44,
                        "Tennessee",
                 ifelse(state_old==45,
                        "Texas",
                        ifelse(state_old==46,
                        "Utah",
                 ifelse(state_old==47,
                        "Vermont",
                 ifelse(state_old==48,
                        "Virginia",
                 ifelse(state_old==49,
                        "Washington",
                 ifelse(state_old==50,
                        "West Virginia",
                        ifelse(state_old==51,
                        "Wisconsin",
                 ifelse(state_old==52,
                        "Wyoming",
                 ifelse(state_old==53,
                        "Other U.S. Territory",
                 ifelse(state_old==54,
                        "I do not reside in the United States",
                 state))))))))))))))))))))))))))))))
            



## Gender

clean.df <- clean.df %>%
  rename(gender_old = gender)

clean.df <- clean.df %>%
  mutate(gender = ifelse(gender_old == 1,
                         "Male",
                  ifelse(gender_old == 2,
                         "Female",
                  ifelse(gender_old == 3,
                         "Non-binary/third gender",
                  ifelse(gender_old == 4,
                         "Prefer not to say",
                         NA)))))

diag <- clean.df %>%
  group_by(gender) %>%
  summarize(count=n())


## Gender by Party ID

diag<-clean.df %>%
  group_by(gender,
           party_id) %>%
  summarize(count=n())



## Age

clean.df <- clean.df %>%
  rename(age_old = age)

clean.df <- clean.df %>%
  mutate(age = ifelse(age_old == 2,
                      "18-24 years old",
               ifelse(age_old == 3,
                      "25-34 years old",
               ifelse(age_old == 4, 
                      "35-44 years old",
               ifelse(age_old==5,
                      "45-54 years old",
               ifelse(age_old == 6,
                      "55-64 years old",
               ifelse(age_old == 7,
                      "65+ years old",
                      NA)))))))

## Race

# check.df <- read.csv("Diss_ Ad Transcripts study 2_January 25, 2022_17.33.csv")
# 
# diag2<-check.df %>%
#   filter(gc==1)%>%
#   group_by(race)%>%
#   summarize(count=n())
# 
# diag<-clean.df %>%
#   group_by(race)%>%
#   summarize(count=n())

clean.df <- clean.df %>%
  rename(race_old = race) 

clean.df <- clean.df %>%
  mutate(race = ifelse(race_old == 8,
                       "Only Latino/Hispanic",
                ifelse(race_old == 5,
                       "Latino and White",
                ifelse(race_old == 3,
                       "Latino and Black",
                "Other/Multiracial"))))

## National origin

## Adding from open responses

clean.df <- clean.df %>%
  mutate(natlorigin_clean = ifelse(natl.origin != "41",
                                   NatOrigin,
                            ifelse(NatOrigin=="Panamian",
                                   "Panamanian",
                            ifelse(NatOrigin %in%c("Africa, PR",
                                                    "Brazil,Puerto rico,etc",
                                                    "Germany , Puerto Rico , United States",
                                                    "Puerto Rico",
                                                    "Puerto Rico Grecian"),
                                   "Puerto Rican",
                            ifelse(NatOrigin %in%c("Both Honduras and Nicrarga"),
                                   "Honduran",
                            ifelse(NatOrigin %in%c("Brazil and Mexico",
                                                    "California/Mexico",
                                                    "Europe Irish Mexico",
                                                    "Filipino/Mexican",
                                                    "Germany, England, Mexico",
                                                    "Germany, Mexico",
                                                    "Greece Germany Mexico",
                                                    "mexico",
                                                    "Mexico",
                                                    "Mexico and Belize",
                                                    "Mexico and Europe",
                                                    "Mexico and Indian so idk really know",
                                                    "Mexico France Germany",
                                                    "Mexico, France",
                                                    "Mexico, Spain, England",
                                                    "MX and Ethiopia"),
                                   "Mexican",
                            ifelse(NatOrigin %in%c("Dominican Republic"),
                                   "Dominican",
                            ifelse(NatOrigin %in%c("El Salvador, Mexico"),
                                   "Salvadoran",
                                   "Other"))))))))


## Fixing misspelling

clean.df <- clean.df%>%
  mutate(natlorigin_clean = ifelse(natl.origin=="35",
                                   "Panamanian",
                                   natlorigin_clean))



## English and Spanish skills
## 1-5


## English

clean.df <- clean.df %>%
  mutate(english_skills = ifelse(english.skills=="5",
                                 "Perfectly",
                          ifelse(english.skills=="1",
                                 "Very well",
                          ifelse(english.skills=="2",
                                 "Somewhat well",
                          ifelse(english.skills=="3",
                                 "Just a little",
                                 NA)))))

## Spanish

clean.df <- clean.df %>%
  mutate(spanish_skills = ifelse(spanish.skills=="5",
                                 "Perfectly",
                          ifelse(spanish.skills=="1",
                                 "Very well",
                          ifelse(spanish.skills=="2",
                                 "Somewhat well",
                          ifelse(spanish.skills=="3",
                                 "Just a little",
                          ifelse(spanish.skills=="4",
                                 "Not at all",
                                 NA))))))

## Panethnic preference

diag <- clean.df %>%
  group_by(panethnic.pref)%>%
  summarize(count=n())

## Immigrant generation

## ## Diagnostics

clean.df %>%
  group_by(imm.nativity,
           imm.parents)%>%
  summarize(count=n())



## ## Just 1st, 2nd, 3rd
clean.df <- clean.df%>%
  mutate(imm_gen = ifelse(imm.nativity=="2",
                          "1st",
                   ifelse(imm.nativity%in%c("1","4") &
                          imm.parents%in%c("3","2"),
                          "2nd",
                          "3rd+")))
                          

## Known Undoc

clean.df <- clean.df %>%
  mutate(undoc_know = ifelse(undoc.know == 1,
                             "None",
                      ifelse(undoc.know == 2,
                             "A few",
                      ifelse(undoc.know == 3,
                             "Some",
                      ifelse(undoc.know == 4,
                             "A lot",
                             NA)))))
clean.df %>%
  group_by(undoc_know)%>%
  summarize(count=n())

## Ideology

##  Ranges from 1 (Extremely liberal) to 9 (Extremely conservative)

clean.df <- clean.df %>%
  mutate(ideology_old = ideology)

clean.df <- clean.df %>%
  mutate(ideology = as.numeric(ideology_old))



## 2020 Vote and vote choice

clean.df <- clean.df %>%
  mutate(vote_2020 = ifelse(vote.2020 =="1",
                            "Yes",
                     ifelse(vote.2020%in%c("2",
                                           "3"),
                            "No",
                            NA)))

clean.df <- clean.df %>%
  mutate(votechoice_2020 = ifelse(votechoice.2020 == "1",
                                  "Donald Trump",
                           ifelse(votechoice.2020 == "2",
                                  "Joe Biden",
                           ifelse(vote_2020 == "No",
                                  "Didn't vote",
                           "Other/Prefer to not say"))))

## Religion

## religion_all: everything

clean.df <- clean.df%>%
  mutate(religion_all = ifelse(religion == "83",
                               "Catholic",
                        ifelse(religion=="85",
                               "Christian",
                        ifelse(religion =="93",
                               "None",
                        ifelse(religion=="92",
                               "Other",
                        ifelse(religion=="90",
                               "Agnostic",
                        ifelse(religion=="91",
                               "Atheist",
                        ifelse(religion=="84",
                               "Protestant",
                        ifelse(religion=="94",
                               "Other Christian denomination",
                        ifelse(religion=="95",
                               "Evangelical",
                        ifelse(religion=="86",
                               "Jewish",
                        ifelse(religion=="89",
                                "Buddhist",
                        ifelse(religion=="87",
                               "Muslim",
                        ifelse(religion=="88",
                               "Hindu",
                               NA))))))))))))))

## Add "Others"

clean.df <- clean.df %>%
  mutate(religion_all = ifelse(religion_all !="Other",
                               religion_all,
                        ifelse(religion.other%in%c("Baptist",
                                                   "Penacostal",
                                                   "Penicostal",
                                                   "Pentacostal",
                                                   "Pentacotal",
                                                   "Pentecostal",
                                                   "Pentecostal "),
                               "Evangelical",
                        ifelse(religion.other%in%c("catholic",
                                                   "Catholic/Christian"),
                               "Catholic",
                        ifelse(religion.other%in%c("Christian",
                                                   "christian",
                                                   "Christan",
                                                   "christian ",
                                                   "Christian ",
                                                   "christian+",
                                                   "Cristian",
                                                   "Don't follow a certain denomination",
                                                   "I am a Christian fellowship",
                                                   "Non-denominational",
                                                   "Non denomination",
                                                   "non denominational",
                                                   "Non denominational",
                                                   "Non dom",
                                                   "Nondenominational"),
                               "Christian",
                        ifelse(religion.other%in%c("Unitarian",
                                                   "Jehova's witnesses",
                                                   "Jehovah  witness",
                                                   "jehovah witness",
                                                   "LDS",
                                                   "Lds",
                                                   "latter day saint",
                                                   "Mormom",
                                                   "Church of Jesus Christ of Latter-day Saints",
                                                   "I am a Member of the Church of Jesus Christ of Latter Day Saints"),
                               "Other Christian denomination",
                        ifelse(religion.other%in%c("None",
                                                   "None ",
                                                   "not religious",
                                                   "Na",
                                                   "No specific religion",
                                                   "nothing",
                                                   "Spiritual",
                                                   "spiritual",
                                                   "Spiritual ",
                                                   "Spiritualist",
                                                   "Spritual"),
                               "None",
                               "Other religion")))))))



## religion_condense: emphasis on Christian vs Catholic vs Protestant/Evangelical vs None/Atheist/Agnostic vs Other

clean.df <- clean.df %>%
  mutate(religion_condense = ifelse(religion_all =="Catholic",
                                    "Catholic",
                             ifelse(religion_all%in%c("Evangelical",
                                                  "Protestant",
                                                  "Other Christian denomination"),
                                    "Evangelical/Protestant/other denomination",
                             ifelse(religion_all == "Christian",
                                    "Christian (nonspecific)",
                             ifelse(religion_all %in% c("Agnostic",
                                                    "Atheist",
                                                    "None"),
                                    "Atheist/Agnostic/None",
                                    "Other religion")))))



## Education

clean.df <- clean.df %>%
  mutate(education_old = education)

## As factor

## Below high school
## High school graduate
## Some college
## Completed college
## Professional degree

clean.df <- clean.df %>%
  mutate(education_factor = ifelse(education_old %in%c("1",
                                                     "2",
                                                     "3"),
                                  "Below high school",
                           ifelse(education_old == "4",
                                  "High school graduate",
                           ifelse(education_old == "5",
                                  "Some college",
                           ifelse(education_old %in% c("6",
                                                       "7"),
                                  "Associates, Bachelors",
                           ifelse(education_old %in% c("8",
                                                       "10"),
                                  "Professional",
                                  "Other"))))))

## As numeric

clean.df <- clean.df %>%
  mutate(education_numeric = ifelse(education_old %in%c("1",
                                                     "2",
                                                     "3"),
                                  1,
                           ifelse(education_old == "4",
                                  2,
                           ifelse(education_old == "5",
                                  3,
                           ifelse(education_old %in% c("6",
                                                       "7"),
                                  4,
                           ifelse(education_old %in% c("8",
                                                       "10"),
                                  5,
                                  NA))))))

## ZIP

## Income

clean.df %>%
  group_by(income) %>%
  summarize(count=n())




#### Quality checks

## Attention

clean.df %>%
  group_by(Attention1) %>%
  summarize(count=n())

## Manipulation

clean.df %>%
  group_by(Attention1,
           manip.fisherparty,
           manip.millerparty)%>%
  summarize(count=n())





## Full sample, Latino linked fate

levels.df <- clean.df %>%
  group_by(lat_linked_fate)%>%
  summarize(count=n())

levels_lat_linked_fate <- c(paste(paste("A great deal (N = ", as.character(levels.df[1,2]), sep=""),")", sep=""),
                         paste(paste("A lot (N = ", as.character(levels.df[3,2]), sep=""),")", sep=""),
                         paste(paste("A moderate amount (N = ", as.character(levels.df[4,2]), sep=""),")", sep=""),
                         paste(paste("A little (N = ", as.character(levels.df[2,2]), sep=""),")", sep=""),
                         paste(paste("None at all (N = ", as.character(levels.df[5,2]), sep=""),")", sep=""))

clean.df <- clean.df %>%
  mutate(lat_linked_fate_old = lat_linked_fate)

clean.df <- clean.df %>%
  mutate(lat_linked_fate = ifelse(lat_linked_fate_old == "A great deal",
                               levels_lat_linked_fate[1],
                        ifelse(lat_linked_fate_old == "A lot",
                               levels_lat_linked_fate[2],
                        ifelse(lat_linked_fate_old == "A moderate amount",
                               levels_lat_linked_fate[3],
                        ifelse(lat_linked_fate_old == "A little",
                               levels_lat_linked_fate[4],
                        ifelse(lat_linked_fate_old == "None at all",
                               levels_lat_linked_fate[5],
                               NA
                               ))))))


clean.df$lat_linked_fate <- factor(clean.df$lat_linked_fate,
                                   levels = c("A great deal (N = 309)",
                                              "A lot (N = 401)",
                                              "A moderate amount (N = 453)",
                                              "A little (N = 173)",
                                              "None at all (N = 239)"))



## Income

clean.df <- clean.df %>%
  mutate(income_numeric = as.numeric(income))




study2.cleaned.df <- clean.df


write.csv(clean.df,
          file = "njev_study2_clean.csv")



```




```{r Study 3 Data clean}

unclean.df <- read_csv("Diss_ study 3 conjoint_May 5, 2023_10.03.csv")

clean.df <- unclean.df %>%
  filter(gc==1)


glimpse(clean.df)


###
# Moderators and Demographics
###


## Party ID


clean.df <- clean.df %>%
  rename(party_id = party.id...150)


diag <- clean.df %>%
  group_by(party_id)%>%
  summarize(count=n())

## National origin

diag <- clean.df %>%
  group_by(natl.origin,
           natl.origin.other)%>%
  summarize(count=n())

clean.df <- clean.df %>%
  mutate(Origin = case_when(
    natl.origin == "Mexico" ~ "Mexico",
    natl.origin == "Puerto Rico" ~ "Puerto Rico",
    natl.origin == "Cuba" ~ "Cuba",
    TRUE ~ "Other"
  ))

diag <- clean.df %>%
  group_by(natl.origin,
           Origin)%>%
  summarize(count=n())

## Immigrant generation

diag <- clean.df %>%
  group_by(imm.nativity,
           imm.parents)%>%
  summarize(count=n())

clean.df <- clean.df %>%
  mutate(imm_gen = ifelse(imm.nativity %in% c("No, not born in the U.S.",
                                               "Born in Puerto Rico"),
                           1,
                    ifelse(imm.nativity == "Born in the U.S." &
                           imm.parents %in% c("One parent born in the U.S.",
                                              "One parent born in Puerto Rico",
                                              "Both parents born in Puerto Rico",
                                              "Neither parent born in the U.S. or Puerto Rico"),
                           2,
                    ifelse(imm.nativity == "Born in the U.S." &
                           imm.parents %in% c("Both parents born in the U.S.",
                                              "Not sure"),
                           3,
                           NA))))

diag <- clean.df %>%
  group_by(imm_gen)%>%
  summarize(count=n())

## Gender

diag <- clean.df %>%
  group_by(gender)%>%
  summarize(count=n())

## State

diag <- clean.df %>%
  group_by(state)%>%
  summarize(count=n())

## Age

diag <- clean.df %>%
  group_by(age)%>%
  summarize(count=n())

## Race

diag <- clean.df %>%
  group_by(race)%>%
  summarize(count=n())

clean.df <- clean.df %>%
  mutate(Race = case_when(
    race %in% c("Latino/Hispanic") ~ "Only Latino",
    race %in% c("Latino/Hispanic,White") ~"Latino and White",
    race %in% c("Latino/Hispanic,Black") ~"Latino and Black",
    TRUE ~ "Other/Multiracial"
  ))

## Panethnic preference

diag <- clean.df %>%
  group_by(panethnic.pref...152)%>%
  summarize(count=n())

clean.df <- clean.df %>%
  rename(panethnic_pref = panethnic.pref...152)

## Latino ID

diag <- clean.df %>%
  group_by(latino.id.latina,
           latino.id.latino,
           latino.id.othera)%>%
  summarize(count=n())

clean.df <- clean.df %>%
  mutate(latino_idstr = ifelse(latino.id.latina == "Not important at all",
                               1,
                        ifelse(latino.id.latina == "Not too important",
                               2,
                        ifelse(latino.id.latina == "Somewhat important",
                               3,
                        ifelse(latino.id.latina == "Very important",
                               4,
                        ifelse(latino.id.latina == "Extremely important",
                               5,
                               NA))))))

clean.df <- clean.df %>%
  mutate(latino_idstr = ifelse(is.na(latino_idstr)==FALSE,
                               latino_idstr,
                        ifelse(latino.id.latino == "Not important at all",
                               1,
                        ifelse(latino.id.latino == "Not too important",
                               2,
                        ifelse(latino.id.latino == "Somewhat important",
                               3,
                        ifelse(latino.id.latino == "Very important",
                               4,
                        ifelse(latino.id.latino == "Extremely important",
                               5,
                               NA)))))))

clean.df <- clean.df %>%
  mutate(latino_idstr = ifelse(is.na(latino_idstr)==FALSE,
                               latino_idstr,
                        ifelse(latino.id.othera == "Not important at all",
                               1,
                        ifelse(latino.id.othera == "Not too important",
                               2,
                        ifelse(latino.id.othera == "Somewhat important",
                               3,
                        ifelse(latino.id.othera == "Very important",
                               4,
                        ifelse(latino.id.othera == "Extremely important",
                               5,
                               NA)))))))


diag <- clean.df %>%
  group_by(latino.id.latina,
           latino.id.latino,
           latino.id.othera,
           latino_idstr)%>%
  summarize(count=n())
         

## Latino perceive discrimination

diag <- clean.df %>%
  group_by(lat.perc.discrim)%>%
  summarize(count=n())

## Latino linked fate

diag <- clean.df %>%
  group_by(lat.linked.fate)%>%
  summarize(count=n())

## Undocumented known

diag <- clean.df %>%
  group_by(undoc.know)%>%
  summarize(count=n())

## Ideology

diag <- clean.df %>%
  group_by(ideology)%>%
  summarize(count=n())

## Political interest

diag <- clean.df %>%
  group_by(pol.interest)%>%
  summarize(count=n())

## 2020 vote choice

diag <- clean.df %>%
  group_by(votechoice.2020)%>%
  summarize(count=n())

## 2022 vote choice

diag <- clean.df %>%
  group_by(votechoice.2022)%>%
  summarize(count=n())

## Religion

diag <- clean.df %>%
  group_by(religion,
           religion.other,
           religion.evangelical)%>%
  summarize(count=n())

## Religiosity

diag <- clean.df %>%
  group_by(religion.attend)%>%
  summarize(count=n())

## Education

diag <- clean.df %>%
  group_by(education)%>%
  summarize(count=n())

## Income

diag <- clean.df %>%
  group_by(income)%>%
  summarize(count=n())

## Spanish language

diag <- clean.df %>%
  group_by(spanish.skills) %>%
  reframe(count=n())

write_csv(clean.df, "study3_demog.csv")


###
# Conjoint analysis variables
###


#### Election 1

## Candidate Race
clean.df <- clean.df %>%
  mutate(e1_dem_race = ifelse(e1c1Race == "White",
                                     "White",
                              ifelse(e1c1Race %in% c("Hispanic",
                                                     "Latino",
                                                     "Latinx"),
                                     "Latino",
                                     NA)),
         e1_rep_race = ifelse(e1c2Race == "White",
                                     "White",
                              ifelse(e1c2Race %in% c("Hispanic",
                                                     "Latino",
                                                     "Latinx"),
                                     "Latino",
                                     NA)))


## Policy positions
clean.df <- clean.df %>%
  mutate(e1_dem_econ = ifelse(e1c1Econ == "Tax the rich to make healthcare and education more affordable",
                                     "Progressive",
                              ifelse(e1c1Econ == "Make healthcare and education more affordable",
                                     "Services",
                              ifelse(e1c1Econ == "Invest in infrastructure like roads and bridges",
                                     "Infrastructure",
                              ifelse(e1c1Econ == "Reduce inflation and support small businesses",
                                     "Inflation/Small Businesses",
                                     "No Policy")))),
         e1_rep_econ = ifelse(e1c2Econ == "Support free markets instead of adopting socialist policies",
                                     "Anti-Socialism",
                              ifelse(e1c2Econ == "Make healthcare and education more affordable",
                                     "Services",
                              ifelse(e1c2Econ == "Invest in infrastructure like roads and bridges",
                                     "Infrastructure",
                              ifelse(e1c2Econ == "Reduce inflation and support small businesses",
                                     "Inflation/Small Businesses",
                                     "No Policy")))),
         e1_dem_immig = ifelse(e1c1Immig == "Give undocumented immigrants a pathway to citizenship",
                                     "Pathway for Undocumented",
                              ifelse(e1c1Immig == "Allow higher numbers of legal immigrants each year",
                                     "More Legal Immigrants",
                              ifelse(e1c1Immig == "Invest in stronger and more modern border security",
                                     "Border Security",
                                     "No Policy"))),
         e1_rep_immig = ifelse(e1c2Immig == "Greatly increase border security to stop illegal aliens and drug traffickers",
                                     "Anti-Undocumented",
                              ifelse(e1c2Immig == "Allow higher numbers of legal immigrants each year",
                                     "More Legal Immigrants",
                              ifelse(e1c2Immig == "Invest in stronger and more modern border security",
                                     "Border Security",
                                     "No Policy"))),
         e1_dem_abortion = ifelse(e1c1Abortion == "Make abortion legal nationally without restrictions",
                                     "Legal Without Restrictions",
                              ifelse(e1c1Abortion == "Make abortion legal nationally until the third trimester (27 weeks)",
                                     "Legal to 3rd Trimester",
                              ifelse(e1c1Abortion== "Make abortion legal nationally until the second trimester (13 weeks)",
                                     "Legal to 2nd Trimester",
                                     "No Policy"))),
         e1_rep_abortion = ifelse(e1c2Abortion == "Make abortion illegal nationally, with exceptions for rape and incest",
                                     "Illegal With Exceptions for Rape/Incest",
                              ifelse(e1c2Abortion == "Make abortion legal nationally until the third trimester (27 weeks)",
                                     "Legal to 3rd Trimester",
                              ifelse(e1c2Abortion== "Make abortion legal nationally up until the second trimester (13 weeks)",
                                     "Legal to 2nd Trimester",
                                     "No Policy"))))



#### Election 2

## Candidate Race
clean.df <- clean.df %>%
  mutate(e2_dem_race = ifelse(e2c1Race == "White",
                                     "White",
                              ifelse(e2c1Race %in% c("Hispanic",
                                                     "Latino",
                                                     "Latinx"),
                                     "Latino",
                                     NA)),
         e2_rep_race = ifelse(e2c2Race == "White",
                                     "White",
                              ifelse(e2c2Race %in% c("Hispanic",
                                                     "Latino",
                                                     "Latinx"),
                                     "Latino",
                                     NA)))


## Policy positions
clean.df <- clean.df %>%
  mutate(e2_dem_econ = ifelse(e2c1Econ == "Tax the rich to make healthcare and education more affordable",
                                     "Progressive",
                              ifelse(e2c1Econ == "Make healthcare and education more affordable",
                                     "Services",
                              ifelse(e2c1Econ == "Invest in infrastructure like roads and bridges",
                                     "Infrastructure",
                              ifelse(e2c1Econ == "Reduce inflation and support small businesses",
                                     "Inflation/Small Businesses",
                                     "No Policy")))),
         e2_rep_econ = ifelse(e2c2Econ == "Support free markets instead of adopting socialist policies",
                                     "Anti-Socialism",
                              ifelse(e2c2Econ == "Make healthcare and education more affordable",
                                     "Services",
                              ifelse(e2c2Econ == "Invest in infrastructure like roads and bridges",
                                     "Infrastructure",
                              ifelse(e2c2Econ == "Reduce inflation and support small businesses",
                                     "Inflation/Small Businesses",
                                     "No Policy")))),
         e2_dem_immig = ifelse(e2c1Immig == "Give undocumented immigrants a pathway to citizenship",
                                     "Pathway for Undocumented",
                              ifelse(e2c1Immig == "Allow higher numbers of legal immigrants each year",
                                     "More Legal Immigrants",
                              ifelse(e2c1Immig == "Invest in stronger and more modern border security",
                                     "Border Security",
                                     "No Policy"))),
         e2_rep_immig = ifelse(e2c2Immig == "Greatly increase border security to stop illegal aliens and drug traffickers",
                                     "Anti-Undocumented",
                              ifelse(e2c2Immig == "Allow higher numbers of legal immigrants each year",
                                     "More Legal Immigrants",
                              ifelse(e2c2Immig == "Invest in stronger and more modern border security",
                                     "Border Security",
                                     "No Policy"))),
         e2_dem_abortion = ifelse(e2c1Abortion == "Make abortion legal nationally without restrictions",
                                     "Legal Without Restrictions",
                              ifelse(e2c1Abortion == "Make abortion legal nationally until the third trimester (27 weeks)",
                                     "Legal to 3rd Trimester",
                              ifelse(e2c1Abortion== "Make abortion legal nationally until the second trimester (13 weeks)",
                                     "Legal to 2nd Trimester",
                                     "No Policy"))),
         e2_rep_abortion = ifelse(e2c2Abortion == "Make abortion illegal nationally, with exceptions for rape and incest",
                                     "Illegal With Exceptions for Rape/Incest",
                              ifelse(e2c2Abortion == "Make abortion legal nationally until the third trimester (27 weeks)",
                                     "Legal to 3rd Trimester",
                              ifelse(e2c2Abortion== "Make abortion legal nationally up until the second trimester (13 weeks)",
                                     "Legal to 2nd Trimester",
                                     "No Policy"))))


# Election 3

## Candidate Race
clean.df <- clean.df %>%
  mutate(e3_dem_race = ifelse(e3c1Race == "White",
                                     "White",
                              ifelse(e3c1Race %in% c("Hispanic",
                                                     "Latino",
                                                     "Latinx"),
                                     "Latino",
                                     NA)),
         e3_rep_race = ifelse(e3c2Race == "White",
                                     "White",
                              ifelse(e3c2Race %in% c("Hispanic",
                                                     "Latino",
                                                     "Latinx"),
                                     "Latino",
                                     NA)))


## Policy positions
clean.df <- clean.df %>%
  mutate(e3_dem_econ = ifelse(e3c1Econ == "Tax the rich to make healthcare and education more affordable",
                                     "Progressive",
                              ifelse(e3c1Econ == "Make healthcare and education more affordable",
                                     "Services",
                              ifelse(e3c1Econ == "Invest in infrastructure like roads and bridges",
                                     "Infrastructure",
                              ifelse(e3c1Econ == "Reduce inflation and support small businesses",
                                     "Inflation/Small Businesses",
                                     "No Policy")))),
         e3_rep_econ = ifelse(e3c2Econ == "Support free markets instead of adopting socialist policies",
                                     "Anti-Socialism",
                              ifelse(e3c2Econ == "Make healthcare and education more affordable",
                                     "Services",
                              ifelse(e3c2Econ == "Invest in infrastructure like roads and bridges",
                                     "Infrastructure",
                              ifelse(e3c2Econ == "Reduce inflation and support small businesses",
                                     "Inflation/Small Businesses",
                                     "No Policy")))),
         e3_dem_immig = ifelse(e3c1Immig == "Give undocumented immigrants a pathway to citizenship",
                                     "Pathway for Undocumented",
                              ifelse(e3c1Immig == "Allow higher numbers of legal immigrants each year",
                                     "More Legal Immigrants",
                              ifelse(e3c1Immig == "Invest in stronger and more modern border security",
                                     "Border Security",
                                     "No Policy"))),
         e3_rep_immig = ifelse(e3c2Immig == "Greatly increase border security to stop illegal aliens and drug traffickers",
                                     "Anti-Undocumented",
                              ifelse(e3c2Immig == "Allow higher numbers of legal immigrants each year",
                                     "More Legal Immigrants",
                              ifelse(e3c2Immig == "Invest in stronger and more modern border security",
                                     "Border Security",
                                     "No Policy"))),
         e3_dem_abortion = ifelse(e3c1Abortion == "Make abortion legal nationally without restrictions",
                                     "Legal Without Restrictions",
                              ifelse(e3c1Abortion == "Make abortion legal nationally until the third trimester (27 weeks)",
                                     "Legal to 3rd Trimester",
                              ifelse(e3c1Abortion== "Make abortion legal nationally until the second trimester (13 weeks)",
                                     "Legal to 2nd Trimester",
                                     "No Policy"))),
         e3_rep_abortion = ifelse(e3c2Abortion == "Make abortion illegal nationally, with exceptions for rape and incest",
                                     "Illegal With Exceptions for Rape/Incest",
                              ifelse(e3c2Abortion == "Make abortion legal nationally until the third trimester (27 weeks)",
                                     "Legal to 3rd Trimester",
                              ifelse(e3c2Abortion== "Make abortion legal nationally up until the second trimester (13 weeks)",
                                     "Legal to 2nd Trimester",
                                     "No Policy"))))


# Election 4

## Candidate Race
clean.df <- clean.df %>%
  mutate(e4_dem_race = ifelse(e4c1Race == "White",
                                     "White",
                              ifelse(e4c1Race %in% c("Hispanic",
                                                     "Latino",
                                                     "Latinx"),
                                     "Latino",
                                     NA)),
         e4_rep_race = ifelse(e4c2Race == "White",
                                     "White",
                              ifelse(e4c2Race %in% c("Hispanic",
                                                     "Latino",
                                                     "Latinx"),
                                     "Latino",
                                     NA)))


## Policy positions
clean.df <- clean.df %>%
  mutate(e4_dem_econ = ifelse(e4c1Econ == "Tax the rich to make healthcare and education more affordable",
                                     "Progressive",
                              ifelse(e4c1Econ == "Make healthcare and education more affordable",
                                     "Services",
                              ifelse(e4c1Econ == "Invest in infrastructure like roads and bridges",
                                     "Infrastructure",
                              ifelse(e4c1Econ == "Reduce inflation and support small businesses",
                                     "Inflation/Small Businesses",
                                     "No Policy")))),
         e4_rep_econ = ifelse(e4c2Econ == "Support free markets instead of adopting socialist policies",
                                     "Anti-Socialism",
                              ifelse(e4c2Econ == "Make healthcare and education more affordable",
                                     "Services",
                              ifelse(e4c2Econ == "Invest in infrastructure like roads and bridges",
                                     "Infrastructure",
                              ifelse(e4c2Econ == "Reduce inflation and support small businesses",
                                     "Inflation/Small Businesses",
                                     "No Policy")))),
         e4_dem_immig = ifelse(e4c1Immig == "Give undocumented immigrants a pathway to citizenship",
                                     "Pathway for Undocumented",
                              ifelse(e4c1Immig == "Allow higher numbers of legal immigrants each year",
                                     "More Legal Immigrants",
                              ifelse(e4c1Immig == "Invest in stronger and more modern border security",
                                     "Border Security",
                                     "No Policy"))),
         e4_rep_immig = ifelse(e4c2Immig == "Greatly increase border security to stop illegal aliens and drug traffickers",
                                     "Anti-Undocumented",
                              ifelse(e4c2Immig == "Allow higher numbers of legal immigrants each year",
                                     "More Legal Immigrants",
                              ifelse(e4c2Immig == "Invest in stronger and more modern border security",
                                     "Border Security",
                                     "No Policy"))),
         e4_dem_abortion = ifelse(e4c1Abortion == "Make abortion legal nationally without restrictions",
                                     "Legal Without Restrictions",
                              ifelse(e4c1Abortion == "Make abortion legal nationally until the third trimester (27 weeks)",
                                     "Legal to 3rd Trimester",
                              ifelse(e4c1Abortion== "Make abortion legal nationally until the second trimester (13 weeks)",
                                     "Legal to 2nd Trimester",
                                     "No Policy"))),
         e4_rep_abortion = ifelse(e4c2Abortion == "Make abortion illegal nationally, with exceptions for rape and incest",
                                     "Illegal With Exceptions for Rape/Incest",
                              ifelse(e4c2Abortion == "Make abortion legal nationally until the third trimester (27 weeks)",
                                     "Legal to 3rd Trimester",
                              ifelse(e4c2Abortion== "Make abortion legal nationally up until the second trimester (13 weeks)",
                                     "Legal to 2nd Trimester",
                                     "No Policy"))))


# Election 5


## Candidate Race
clean.df <- clean.df %>%
  mutate(e5_dem_race = ifelse(e5c1Race == "White",
                                     "White",
                              ifelse(e5c1Race %in% c("Hispanic",
                                                     "Latino",
                                                     "Latinx"),
                                     "Latino",
                                     NA)),
         e5_rep_race = ifelse(e5c2Race == "White",
                                     "White",
                              ifelse(e5c2Race %in% c("Hispanic",
                                                     "Latino",
                                                     "Latinx"),
                                     "Latino",
                                     NA)))


## Policy positions
clean.df <- clean.df %>%
  mutate(e5_dem_econ = ifelse(e5c1Econ == "Tax the rich to make healthcare and education more affordable",
                                     "Progressive",
                              ifelse(e5c1Econ == "Make healthcare and education more affordable",
                                     "Services",
                              ifelse(e5c1Econ == "Invest in infrastructure like roads and bridges",
                                     "Infrastructure",
                              ifelse(e5c1Econ == "Reduce inflation and support small businesses",
                                     "Inflation/Small Businesses",
                                     "No Policy")))),
         e5_rep_econ = ifelse(e5c2Econ == "Support free markets instead of adopting socialist policies",
                                     "Anti-Socialism",
                              ifelse(e5c2Econ == "Make healthcare and education more affordable",
                                     "Services",
                              ifelse(e5c2Econ == "Invest in infrastructure like roads and bridges",
                                     "Infrastructure",
                              ifelse(e5c2Econ == "Reduce inflation and support small businesses",
                                     "Inflation/Small Businesses",
                                     "No Policy")))),
         e5_dem_immig = ifelse(e5c1Immig == "Give undocumented immigrants a pathway to citizenship",
                                     "Pathway for Undocumented",
                              ifelse(e5c1Immig == "Allow higher numbers of legal immigrants each year",
                                     "More Legal Immigrants",
                              ifelse(e5c1Immig == "Invest in stronger and more modern border security",
                                     "Border Security",
                                     "No Policy"))),
         e5_rep_immig = ifelse(e5c2Immig == "Greatly increase border security to stop illegal aliens and drug traffickers",
                                     "Anti-Undocumented",
                              ifelse(e5c2Immig == "Allow higher numbers of legal immigrants each year",
                                     "More Legal Immigrants",
                              ifelse(e5c2Immig == "Invest in stronger and more modern border security",
                                     "Border Security",
                                     "No Policy"))),
         e5_dem_abortion = ifelse(e5c1Abortion == "Make abortion legal nationally without restrictions",
                                     "Legal Without Restrictions",
                              ifelse(e5c1Abortion == "Make abortion legal nationally until the third trimester (27 weeks)",
                                     "Legal to 3rd Trimester",
                              ifelse(e5c1Abortion== "Make abortion legal nationally until the second trimester (13 weeks)",
                                     "Legal to 2nd Trimester",
                                     "No Policy"))),
         e5_rep_abortion = ifelse(e5c2Abortion == "Make abortion illegal nationally, with exceptions for rape and incest",
                                     "Illegal With Exceptions for Rape/Incest",
                              ifelse(e5c2Abortion == "Make abortion legal nationally until the third trimester (27 weeks)",
                                     "Legal to 3rd Trimester",
                              ifelse(e5c2Abortion== "Make abortion legal nationally up until the second trimester (13 weeks)",
                                     "Legal to 2nd Trimester",
                                     "No Policy"))))


# Election 6

## Candidate Race
clean.df <- clean.df %>%
  mutate(e6_dem_race = ifelse(e6c1Race == "White",
                                     "White",
                              ifelse(e6c1Race %in% c("Hispanic",
                                                     "Latino",
                                                     "Latinx"),
                                     "Latino",
                                     NA)),
         e6_rep_race = ifelse(e6c2Race == "White",
                                     "White",
                              ifelse(e6c2Race %in% c("Hispanic",
                                                     "Latino",
                                                     "Latinx"),
                                     "Latino",
                                     NA)))


## Policy positions
clean.df <- clean.df %>%
  mutate(e6_dem_econ = ifelse(e6c1Econ == "Tax the rich to make healthcare and education more affordable",
                                     "Progressive",
                              ifelse(e6c1Econ == "Make healthcare and education more affordable",
                                     "Services",
                              ifelse(e6c1Econ == "Invest in infrastructure like roads and bridges",
                                     "Infrastructure",
                              ifelse(e6c1Econ == "Reduce inflation and support small businesses",
                                     "Inflation/Small Businesses",
                                     "No Policy")))),
         e6_rep_econ = ifelse(e6c2Econ == "Support free markets instead of adopting socialist policies",
                                     "Anti-Socialism",
                              ifelse(e6c2Econ == "Make healthcare and education more affordable",
                                     "Services",
                              ifelse(e6c2Econ == "Invest in infrastructure like roads and bridges",
                                     "Infrastructure",
                              ifelse(e6c2Econ == "Reduce inflation and support small businesses",
                                     "Inflation/Small Businesses",
                                     "No Policy")))),
         e6_dem_immig = ifelse(e6c1Immig == "Give undocumented immigrants a pathway to citizenship",
                                     "Pathway for Undocumented",
                              ifelse(e6c1Immig == "Allow higher numbers of legal immigrants each year",
                                     "More Legal Immigrants",
                              ifelse(e6c1Immig == "Invest in stronger and more modern border security",
                                     "Border Security",
                                     "No Policy"))),
         e6_rep_immig = ifelse(e6c2Immig == "Greatly increase border security to stop illegal aliens and drug traffickers",
                                     "Anti-Undocumented",
                              ifelse(e6c2Immig == "Allow higher numbers of legal immigrants each year",
                                     "More Legal Immigrants",
                              ifelse(e6c2Immig == "Invest in stronger and more modern border security",
                                     "Border Security",
                                     "No Policy"))),
         e6_dem_abortion = ifelse(e6c1Abortion == "Make abortion legal nationally without restrictions",
                                     "Legal Without Restrictions",
                              ifelse(e6c1Abortion == "Make abortion legal nationally until the third trimester (27 weeks)",
                                     "Legal to 3rd Trimester",
                              ifelse(e6c1Abortion== "Make abortion legal nationally until the second trimester (13 weeks)",
                                     "Legal to 2nd Trimester",
                                     "No Policy"))),
         e6_rep_abortion = ifelse(e6c2Abortion == "Make abortion illegal nationally, with exceptions for rape and incest",
                                     "Illegal With Exceptions for Rape/Incest",
                              ifelse(e6c2Abortion == "Make abortion legal nationally until the third trimester (27 weeks)",
                                     "Legal to 3rd Trimester",
                              ifelse(e6c2Abortion== "Make abortion legal nationally up until the second trimester (13 weeks)",
                                     "Legal to 2nd Trimester",
                                     "No Policy"))))



## Outcome variables

clean.df <- clean.df %>%
  mutate(dv_e1_demvote = ifelse(Cand1 == "D",
                                ifelse(election1_choice == "Candidate 1",
                                       1,
                                       0),
                         ifelse(Cand1 == "R",
                                ifelse(election1_choice == "Candidate 2",
                                       1,
                                       0),
                                NA)),
         dv_e1_repvote = ifelse(Cand1 == "R",
                                ifelse(election1_choice == "Candidate 1",
                                       1,
                                       0),
                                ifelse(Cand1 == "D",
                                ifelse(election1_choice == "Candidate 2",
                                       1,
                                       0),
                                NA)),
         dv_e2_demvote = ifelse(Cand1 == "D",
                                ifelse(election2_choice == "Candidate 1",
                                       1,
                                       0),
                                ifelse(Cand1 == "R",
                                ifelse(election2_choice == "Candidate 2",
                                       1,
                                       0),
                                NA)),
         dv_e2_repvote = ifelse(Cand1 == "R",
                                ifelse(election2_choice == "Candidate 1",
                                       1,
                                       0),
                                ifelse(Cand1 == "D",
                                ifelse(election2_choice == "Candidate 2",
                                       1,
                                       0),
                                NA)),
         dv_e3_demvote = ifelse(Cand1 == "D",
                                ifelse(election1_choice == "Candidate 1",
                                       1,
                                       0),
                                ifelse(Cand1 == "R",
                                ifelse(election1_choice == "Candidate 2",
                                       1,
                                       0),
                                NA)),
         dv_e3_repvote = ifelse(Cand1 == "R",
                                ifelse(election3_choice == "Candidate 1",
                                       1,
                                       0),
                                ifelse(Cand1 == "D",
                                ifelse(election3_choice == "Candidate 2",
                                       1,
                                       0),
                                NA)),
         dv_e4_demvote = ifelse(Cand1 == "D",
                                ifelse(election4_choice == "Candidate 1",
                                       1,
                                       0),
                                ifelse(Cand1 == "R",
                                ifelse(election4_choice == "Candidate 2",
                                       1,
                                       0),
                                NA)),
         dv_e4_repvote = ifelse(Cand1 == "R",
                                ifelse(election4_choice == "Candidate 1",
                                       1,
                                       0),
                                ifelse(Cand1 == "D",
                                ifelse(election4_choice == "Candidate 2",
                                       1,
                                       0),
                                NA)),
         dv_e5_demvote = ifelse(Cand1 == "D",
                                ifelse(election5_choice == "Candidate 1",
                                       1,
                                       0),
                                ifelse(Cand1 == "R",
                                ifelse(election5_choice == "Candidate 2",
                                       1,
                                       0),
                                NA)),
         dv_e5_repvote = ifelse(Cand1 == "R",
                                ifelse(election5_choice == "Candidate 1",
                                       1,
                                       0),
                                ifelse(Cand1 == "D",
                                ifelse(election5_choice == "Candidate 2",
                                       1,
                                       0),
                                NA)),
         dv_e6_demvote = ifelse(Cand1 == "D",
                                ifelse(election6_choice == "Candidate 1",
                                       1,
                                       0),
                                ifelse(Cand1 == "R",
                                ifelse(election6_choice == "Candidate 2",
                                       1,
                                       0),
                                NA)),
         dv_e6_repvote = ifelse(Cand1 == "R",
                                ifelse(election6_choice == "Candidate 1",
                                       1,
                                       0),
                                ifelse(Cand1 == "D",
                                ifelse(election6_choice == "Candidate 2",
                                       1,
                                       0),
                                NA))
  )



write_csv(clean.df,
          file = "njev_study3_clean.csv")



```



```{r Study 3 Conjoint dataframe}

## Creating new tibbles for each candidate/election, then will recombine

e1.dem.df <- tibble(
  ResponseId = clean.df$ResponseId,
  `Party ID` = clean.df$party_id,
  Election = c(rep(1)),
  Party = c(rep("Democrat")),
  Race = clean.df$e1_dem_race,
  Economics = clean.df$e1_dem_econ,
  Immigration = clean.df$e1_dem_immig,
  Abortion = clean.df$e1_dem_abortion,
  Vote = clean.df$dv_e1_demvote,
  `Linked Fate` = clean.df$lat.linked.fate,
  `R Race` = clean.df$Race,
  Spanish = clean.df$spanish.skills,
  Origin = clean.df$Origin
)

e2.dem.df <- tibble(
  ResponseId = clean.df$ResponseId,
  `Party ID` = clean.df$party_id,
  Election = c(rep(2)),
  Party = c(rep("Democrat")),
  Race = clean.df$e2_dem_race,
  Economics = clean.df$e2_dem_econ,
  Immigration = clean.df$e2_dem_immig,
  Abortion = clean.df$e2_dem_abortion,
  Vote = clean.df$dv_e2_demvote,
  `Linked Fate` = clean.df$lat.linked.fate,
  `R Race` = clean.df$Race,
  Spanish = clean.df$spanish.skills,
  Origin = clean.df$Origin
)


e3.dem.df <- tibble(
  ResponseId = clean.df$ResponseId,
  `Party ID` = clean.df$party_id,
  Election = c(rep(3)),
  Party = c(rep("Democrat")),
  Race = clean.df$e3_dem_race,
  Economics = clean.df$e3_dem_econ,
  Immigration = clean.df$e3_dem_immig,
  Abortion = clean.df$e3_dem_abortion,
  Vote = clean.df$dv_e3_demvote,
  `Linked Fate` = clean.df$lat.linked.fate,
  `R Race` = clean.df$Race,
  Spanish = clean.df$spanish.skills,
  Origin = clean.df$Origin
)

e4.dem.df <- tibble(
  ResponseId = clean.df$ResponseId,
  `Party ID` = clean.df$party_id,
  Election = c(rep(4)),
  Party = c(rep("Democrat")),
  Race = clean.df$e4_dem_race,
  Economics = clean.df$e4_dem_econ,
  Immigration = clean.df$e4_dem_immig,
  Abortion = clean.df$e4_dem_abortion,
  Vote = clean.df$dv_e4_demvote,
  `Linked Fate` = clean.df$lat.linked.fate,
  `R Race` = clean.df$Race,
  Spanish = clean.df$spanish.skills,
  Origin = clean.df$Origin
)

e5.dem.df <- tibble(
  ResponseId = clean.df$ResponseId,
  `Party ID` = clean.df$party_id,
  Election = c(rep(5)),
  Party = c(rep("Democrat")),
  Race = clean.df$e5_dem_race,
  Economics = clean.df$e5_dem_econ,
  Immigration = clean.df$e5_dem_immig,
  Abortion = clean.df$e5_dem_abortion,
  Vote = clean.df$dv_e5_demvote,
  `Linked Fate` = clean.df$lat.linked.fate,
  `R Race` = clean.df$Race,
  Spanish = clean.df$spanish.skills,
  Origin = clean.df$Origin
)

e6.dem.df <- tibble(
  ResponseId = clean.df$ResponseId,
  `Party ID` = clean.df$party_id,
  Election = c(rep(6)),
  Party = c(rep("Democrat")),
  Race = clean.df$e6_dem_race,
  Economics = clean.df$e6_dem_econ,
  Immigration = clean.df$e6_dem_immig,
  Abortion = clean.df$e6_dem_abortion,
  Vote = clean.df$dv_e6_demvote,
  `Linked Fate` = clean.df$lat.linked.fate,
  `R Race` = clean.df$Race,
  Spanish = clean.df$spanish.skills,
  Origin = clean.df$Origin
)



e1.rep.df <- tibble(
  ResponseId = clean.df$ResponseId,
  `Party ID` = clean.df$party_id,
  Election = c(rep(1)),
  Party = c(rep("Republican")),
  Race = clean.df$e1_rep_race,
  Economics = clean.df$e1_rep_econ,
  Immigration = clean.df$e1_rep_immig,
  Abortion = clean.df$e1_rep_abortion,
  Vote = clean.df$dv_e1_repvote,
  `Linked Fate` = clean.df$lat.linked.fate,
  `R Race` = clean.df$Race,
  Spanish = clean.df$spanish.skills,
  Origin = clean.df$Origin
)

e2.rep.df <- tibble(
  ResponseId = clean.df$ResponseId,
  `Party ID` = clean.df$party_id,
  Election = c(rep(2)),
  Party = c(rep("Republican")),
  Race = clean.df$e2_rep_race,
  Economics = clean.df$e2_rep_econ,
  Immigration = clean.df$e2_rep_immig,
  Abortion = clean.df$e2_rep_abortion,
  Vote = clean.df$dv_e2_repvote,
  `Linked Fate` = clean.df$lat.linked.fate,
  `R Race` = clean.df$Race,
  Spanish = clean.df$spanish.skills,
  Origin = clean.df$Origin
)


e3.rep.df <- tibble(
  ResponseId = clean.df$ResponseId,
  `Party ID` = clean.df$party_id,
  Election = c(rep(3)),
  Party = c(rep("Republican")),
  Race = clean.df$e3_rep_race,
  Economics = clean.df$e3_rep_econ,
  Immigration = clean.df$e3_rep_immig,
  Abortion = clean.df$e3_rep_abortion,
  Vote = clean.df$dv_e3_repvote,
  `Linked Fate` = clean.df$lat.linked.fate,
  `R Race` = clean.df$Race,
  Spanish = clean.df$spanish.skills,
  Origin = clean.df$Origin
)

e4.rep.df <- tibble(
  ResponseId = clean.df$ResponseId,
  `Party ID` = clean.df$party_id,
  Election = c(rep(4)),
  Party = c(rep("Republican")),
  Race = clean.df$e4_rep_race,
  Economics = clean.df$e4_rep_econ,
  Immigration = clean.df$e4_rep_immig,
  Abortion = clean.df$e4_rep_abortion,
  Vote = clean.df$dv_e4_repvote,
  `Linked Fate` = clean.df$lat.linked.fate,
  `R Race` = clean.df$Race,
  Spanish = clean.df$spanish.skills,
  Origin = clean.df$Origin
)

e5.rep.df <- tibble(
  ResponseId = clean.df$ResponseId,
  `Party ID` = clean.df$party_id,
  Election = c(rep(5)),
  Party = c(rep("Republican")),
  Race = clean.df$e5_rep_race,
  Economics = clean.df$e5_rep_econ,
  Immigration = clean.df$e5_rep_immig,
  Abortion = clean.df$e5_rep_abortion,
  Vote = clean.df$dv_e5_repvote,
  `Linked Fate` = clean.df$lat.linked.fate,
  `R Race` = clean.df$Race,
  Spanish = clean.df$spanish.skills,
  Origin = clean.df$Origin
)

e6.rep.df <- tibble(
  ResponseId = clean.df$ResponseId,
  `Party ID` = clean.df$party_id,
  Election = c(rep(6)),
  Party = c(rep("Republican")),
  Race = clean.df$e6_rep_race,
  Economics = clean.df$e6_rep_econ,
  Immigration = clean.df$e6_rep_immig,
  Abortion = clean.df$e6_rep_abortion,
  Vote = clean.df$dv_e6_repvote,
  `Linked Fate` = clean.df$lat.linked.fate,
  `R Race` = clean.df$Race,
  Spanish = clean.df$spanish.skills,
  Origin = clean.df$Origin
)



conjoint.df <- rbind(e1.dem.df,
                     e2.dem.df,
                     e3.dem.df,
                     e4.dem.df,
                     e5.dem.df,
                     e6.dem.df,
                     e1.rep.df,
                     e2.rep.df,
                     e3.rep.df,
                     e4.rep.df,
                     e5.rep.df,
                     e6.rep.df)

## In the soft launch (N = 60) there were issues with Election 6
## Dropping responses that failed to display candidate  profiles properly

diag <- conjoint.df %>%
  group_by(Race) %>%
  summarize(count = n())

conjoint.df <- conjoint.df %>%
  filter(is.na(Race)==FALSE)



## Conjoint diagnostics


diag <- conjoint.df %>%
  group_by(Party,
           Race)%>%
  summarize(count=n())

diag<- conjoint.df %>%
  group_by(Party,
           Economics)%>%
  summarize(count=n())

diag<-conjoint.df %>%
  group_by(Party,
           Immigration)%>%
  summarize(count=n())

diag<-conjoint.df %>%
  group_by(Party,
           Abortion)%>%
  summarize(count=n())


write_csv(conjoint.df,
          file = "njev_study3_conjointdata.csv")


```






```{r Data load}

## Change working directory as needed
## setwd("C:/Users/ahmis/Dropbox/Articles/Not Just Ethnic Voters/R files")

study1.cleaned.df <- read.csv("njev_study1_clean.csv")

study2.cleaned.df <- read.csv("njev_study2_clean.csv")

study3.cleaned.df <- read.csv("njev_study3_clean.csv")

conjoint.df <- read.csv("njev_study3_conjointdata.csv")

```



```{r Function calls}
`%nin%` <- Negate(`%in%`)


## se_cluster
## Calculates clustered SEs at respondent level

se_cluster <- function(x) {
  vcov_mat <- vcovCL(x, cluster = ~ResponseId)
  lm.clustered <- coeftest(x, vcov = vcov_mat)[, "Std. Error"]
}


robustsummary <- function(model) {
  library(sandwich)
  library(lmtest)
  coeftest <- coeftest(model, vcov = vcovHC(model, type = "HC1"))
  summ <- summary(model)
  summ$coefficients[, 2] <- coeftest[, 2]
  summ$coefficients[, 3] <- coeftest[, 3]
  summ$coefficients[, 4] <- coeftest[, 4]
  summ
}


## withinsubject_ols
## Calculates OLS model for within-subject
## Estimates OLS within-subject

withinsubject_demcand_ols <- function(pid, message) {
  df1 <- dempooled.df %>% filter(party_id == pid)

  tempdf_a <- df1 %>%
    filter(t_2 == message)

  tempdf_b <- df1 %>%
    filter(
      t_2 == "Control",
      ResponseId %in% tempdf_a$ResponseId
    )

  tempdf <- rbind(
    tempdf_a,
    tempdf_b
  )

  ols_temp <- lm(dv_ad2 ~ t_2,
    data = tempdf
  )

  summ <- summary(ols_temp)

  return(summ)
}



withinsubject_repcand_ols <- function(pid, message) {
  df1 <- reppooled.df %>% filter(party_id == pid)

  tempdf_a <- df1 %>%
    filter(t_7 == message)

  tempdf_b <- df1 %>%
    filter(
      t_7 == "Control",
      ResponseId %in% tempdf_a$ResponseId
    )

  tempdf <- rbind(
    tempdf_a,
    tempdf_b
  )

  ols_temp <- lm(dv_ad7 ~ t_7,
    data = tempdf
  )

  return(ols_temp)
}


```



## Study 1



```{r Study 1 Figure 1}

diag <- study1.cleaned.df %>%
  group_by(Treatment) %>%
  summarize(count = n())

diag <- study1.cleaned.df %>%
  group_by(vote.dv) %>%
  summarize(count = n())


## Main: OLS

## Just using bivariate model

temp.dem.df <- study1.cleaned.df %>%
  filter(Party == "Democrat")

temp.ind.df <- study1.cleaned.df %>%
  filter(Party == "Independent")


## First model: Bivariate DV ~ Treatment

## DV: likelihood of vote

ols1 <- lm(fishervote.5pt ~ Treatment,
  data = temp.dem.df
)

ols_coef1 <- coeftest(ols1)

ols2 <- lm(fishervote.5pt ~ Treatment,
  data = temp.ind.df
)

ols_coef2 <- coeftest(ols2)



## Show treatment means

demcand.plot.df <- study1.cleaned.df %>%
  group_by(Treatment, Party) %>%
  summarize(
    count = n(),
    fishervote = sum(fishervote.5pt, na.rm = TRUE) / count
  )


demcand.plot.df$Treatment <- factor(demcand.plot.df$Treatment,
  levels = c(
    "Control",
    "Pro-Undoc",
    "Progressive",
    "Race-Class"
  )
)


demcand.plot.df$se <- c(
  ols_coef1[1, 2],
  ols_coef2[1, 2],
  ols_coef1[2, 2],
  ols_coef2[2, 2],
  ols_coef1[3, 2],
  ols_coef2[3, 2],
  ols_coef1[4, 2],
  ols_coef2[4, 2]
)

ggplot(
  data = demcand.plot.df,
  aes(
    x = Treatment,
    y = fishervote,
    fill = Treatment
  )
) +
  geom_bar(stat = "identity") +
  geom_errorbar(
    aes(
      x = Treatment,
      ymin = fishervote - se,
      ymax = fishervote + se
    ),
    width = 0.2,
    position = position_dodge(.9),
    color = "black"
  ) +
  labs(
    y = "Hypothetical Vote for Democrat Mark Fisher (1-5)",
    x = "Message Treatments"
  ) +
  # scale_fill_grey(start = 0.3, end = 0.7)+
  theme(legend.position = "none") +
  facet_wrap(~Party, ncol = 2) +
  coord_cartesian(ylim = c(1, 5))

ggsave("Final/study1_votemeans_bypid.jpg",
  width = 8,
  height = 4
)
```



```{r Study 1 Appendix}
## Appendix now: OLR

## Make outcome variable a factor; this keeps breaking when loading .csv

study1.cleaned.df$vote.dv <- factor(study1.cleaned.df$vote.dv,
  levels = c(
    "Extremely unlikely",
    "Unlikely",
    "Neither likely nor unlikely",
    "Likely",
    "Extremely likely"
  )
)

## Just using bivariate model

temp.dem.df <- study1.cleaned.df %>%
  filter(Party == "Democrat")

temp.ind.df <- study1.cleaned.df %>%
  filter(Party == "Independent")


## First model: Bivariate DV ~ Treatment

## DV: likelihood of vote

olr1 <- polr(vote.dv ~ Treatment,
  data = temp.dem.df,
  Hess = TRUE
)

olr_coef1 <- coeftest(olr1)

olr2 <- polr(vote.dv ~ Treatment,
  data = temp.ind.df,
  Hess = TRUE
)

olr_coef2 <- coeftest(olr2)



## Show treatment means

demcand.plot.df <- study1.cleaned.df %>%
  group_by(Treatment, Party) %>%
  summarize(
    count = n(),
    fishervote = sum(fishervote.5pt, na.rm = TRUE) / count
  )


demcand.plot.df$Treatment <- factor(demcand.plot.df$Treatment,
  levels = c(
    "Control",
    "Pro-Undoc",
    "Progressive",
    "Race-Class"
  )
)


demcand.plot.df$se <- c(
  0,
  0,
  olr_coef1[1, 2],
  olr_coef2[1, 2],
  olr_coef1[2, 2],
  olr_coef2[2, 2],
  olr_coef1[3, 2],
  olr_coef2[3, 2]
)

ggplot(
  data = demcand.plot.df,
  aes(
    x = Treatment,
    y = fishervote,
    fill = Treatment
  )
) +
  geom_bar(stat = "identity") +
  geom_errorbar(
    aes(
      x = Treatment,
      ymin = fishervote - se,
      ymax = fishervote + se
    ),
    width = 0.2,
    position = position_dodge(.9),
    color = "black"
  ) +
  labs(
    y = "Hypothetical Vote for Democrat Mark Fisher (1-5)",
    x = "Message Treatments"
  ) +
  # scale_fill_grey(start = 0.3, end = 0.7)+
  theme(legend.position = "none") +
  facet_wrap(~Party, ncol = 2) +
  coord_cartesian(ylim = c(1, 5))

ggsave("Final/Appendix/study1_votemeans_olr_bypid.jpg",
  width = 8,
  height = 4
)


### Dependent variable: Turnout

## First model: Bivariate DV ~ Treatment

## DV: Turnout

temp.dem.df <- study1.cleaned.df %>%
  filter(Party == "Democrat")

temp.ind.df <- study1.cleaned.df %>%
  filter(Party == "Independent")



ols1 <- lm(turnout.dv ~ Treatment,
  data = temp.dem.df,
  Hess = TRUE
)

ols_coef1 <- coeftest(ols1)

ols2 <- lm(turnout.dv ~ Treatment,
  data = temp.ind.df,
  Hess = TRUE
)

ols_coef2 <- coeftest(ols2)



## Show treatment means

demcand.plot.df <- study1.cleaned.df %>%
  group_by(Treatment, Party) %>%
  summarize(
    count = n(),
    turnout = sum(as.numeric(turnout.dv), na.rm = TRUE) / count
  )


demcand.plot.df$Treatment <- factor(demcand.plot.df$Treatment,
  levels = c(
    "Control",
    "Pro-Undoc",
    "Progressive",
    "Race-Class"
  )
)


demcand.plot.df$se <- c(
  ols_coef1[1, 2],
  ols_coef2[1, 2],
  ols_coef1[2, 2],
  ols_coef2[2, 2],
  ols_coef1[3, 2],
  ols_coef2[3, 2],
  ols_coef1[4, 2],
  ols_coef2[4, 2]
)

ggplot(
  data = demcand.plot.df,
  aes(
    x = Treatment,
    y = turnout,
    fill = Treatment
  )
) +
  geom_bar(stat = "identity") +
  geom_errorbar(
    aes(
      x = Treatment,
      ymin = turnout - se,
      ymax = turnout + se
    ),
    width = 0.2,
    position = position_dodge(.9),
    color = "black"
  ) +
  labs(
    y = "Hypothetical Turnout to Vote (1-5)",
    x = "Message Treatments"
  ) +
  # scale_fill_grey(start = 0.3, end = 0.7)+
  theme(legend.position = "none") +
  facet_wrap(~Party, ncol = 2) +
  coord_cartesian(ylim = c(1, 5))

ggsave("Final/Appendix/study1_turnoutmeans_bypid.jpg",
  width = 8,
  height = 4
)

# Table 1: OLS Party ID

## Full sample

ols1 <- lm(fishervote.5pt ~ Treatment,
  data = study1.cleaned.df
)

## Add PID dummy

ols2 <- lm(fishervote.5pt ~ Treatment + Party,
  data = study1.cleaned.df
)


## Add PID interaction

ols3 <- lm(fishervote.5pt ~ Treatment * Party,
  data = study1.cleaned.df
)

stargazer(ols1, ols2, ols3,
  out = "Final/Appendix/study1_table.tex",
  dep.var.caption = "DV: Desire to Vote for Candidate",
  dep.var.labels = "",
  covariate.labels = c(
    "Pro-Undoc.",
    "Progressive Econ",
    "Race-Class Econ.",
    "Independent",
    "Independent * Pro-Undoc.",
    "Independent * Progressive Econ.",
    "Independent * Race-Class Econ."
  )
)



## Linked fate descriptive

partyidbylinkedfate.df <- study1.cleaned.df %>%
  group_by(
    Party,
    LatLinkedFate
  ) %>%
  summarize(count = n()) %>%
  rename(`Latino Linked Fate` = LatLinkedFate)

## Fix factors
## Go from low to high

partyidbylinkedfate.df$`Latino Linked Fate` <- factor(partyidbylinkedfate.df$`Latino Linked Fate`,
  levels = c(
    "None at all",
    "A little",
    "A moderate amount",
    "A lot",
    "A great deal"
  )
)


ggplot(
  data = partyidbylinkedfate.df,
  aes(
    x = `Latino Linked Fate`,
    y = count,
    fill = `Latino Linked Fate`
  )
) +
  geom_bar(stat = "identity") +
  facet_wrap(~Party,
    ncol = 2
  ) +
  theme(axis.text.x = element_blank()) +
  labs(
    x = "Response to Latino Linked Fate Question by Respondent Party ID",
    y = "Number of Respondents"
  ) +
  theme(
    legend.title = element_text(size = 18),
    legend.text = element_text(size = 16)
  )

ggsave("Final/Appendix/study1_latlinkedfate_by_partyid.png",
  height = 6,
  width = 10
)


## Does treatment assignment affect manipulations?

## Clean "ManipRace" variable, which captures perception of Fisher's race

study1.cleaned.df %>%
  group_by(
    ManipRace,
    Treatment
  ) %>%
  summarize(count = n())

## Not noticeably


####### Effects by certain demographic characteristics/variables

## Linked fate

study1.cleaned.df <- study1.cleaned.df %>%
  mutate(LatLinkedFate.3pt = case_when(
    LatLinkedFate.5pt == 5 ~ "A great deal",
    LatLinkedFate.5pt == 4 ~ "A lot",
    LatLinkedFate.5pt == 3 ~ "A moderate amount",
    LatLinkedFate.5pt == 2 ~ "A little",
    LatLinkedFate.5pt == 1 ~ "None at all"
  ))

study1.cleaned.df$LatLinkedFate.3pt <- factor(study1.cleaned.df$LatLinkedFate.3pt,
                                              levels = c("None at all",
                                                         "A little",
                                                         "A moderate amount",
                                                         "A lot",
                                                         "A great deal"))

temp.lf5.df <- study1.cleaned.df %>%
  filter(LatLinkedFate.5pt == 5)

temp.lf4.df <- study1.cleaned.df %>%
  filter(LatLinkedFate.5pt == 4)

temp.lf3.df <- study1.cleaned.df %>%
  filter(LatLinkedFate.5pt == 3)

temp.lf2.df <- study1.cleaned.df %>%
  filter(LatLinkedFate.5pt == 2)

temp.lf1.df <- study1.cleaned.df %>%
  filter(LatLinkedFate.5pt == 1)

## DV: likelihood of vote

ols1 <- lm(fishervote.5pt ~ Treatment,
  data = temp.lf1.df
)

ols_coef1 <- coeftest(ols1)


ols2 <- lm(fishervote.5pt ~ Treatment,
  data = temp.lf2.df
)

ols_coef2 <- coeftest(ols2)

ols3 <- lm(fishervote.5pt ~ Treatment,
  data = temp.lf3.df
)

ols_coef3 <- coeftest(ols3)

ols4 <- lm(fishervote.5pt ~ Treatment,
  data = temp.lf4.df
)

ols_coef4 <- coeftest(ols4)

ols5 <- lm(fishervote.5pt ~ Treatment,
  data = temp.lf5.df
)

ols_coef5 <- coeftest(ols5)


## Show treatment means

demcand.plot.df <- study1.cleaned.df %>%
  group_by(Treatment, LatLinkedFate.3pt) %>%
  summarize(
    count = n(),
    fishervote = sum(fishervote.5pt, na.rm = TRUE) / count
  )


demcand.plot.df$Treatment <- factor(demcand.plot.df$Treatment,
  levels = c(
    "Control",
    "Pro-Undoc",
    "Progressive",
    "Race-Class"
  )
)


demcand.plot.df$se <- c(
  ols_coef1[1, 2],
  ols_coef2[1, 2],
  ols_coef3[1, 2],
  ols_coef4[1, 2],
  ols_coef5[1, 2],
  ols_coef1[2, 2],
  ols_coef2[2, 2],
  ols_coef3[2, 2],
  ols_coef4[2, 2],
  ols_coef5[2, 2],
  ols_coef1[3, 2],
  ols_coef2[3, 2],
  ols_coef3[3, 2],
  ols_coef4[3, 2],
  ols_coef5[3, 2],
  ols_coef1[4, 2],
  ols_coef2[4, 2],
  ols_coef3[4, 2],
  ols_coef4[4, 2],
  ols_coef5[4, 2]
)

ggplot(
  data = demcand.plot.df,
  aes(
    x = Treatment,
    y = fishervote,
    fill = Treatment
  )
) +
  geom_bar(stat = "identity") +
  geom_errorbar(
    aes(
      x = Treatment,
      ymin = fishervote - se,
      ymax = fishervote + se
    ),
    width = 0.2,
    position = position_dodge(.9),
    color = "black"
  ) +
  labs(
    y = "Hypothetical Vote for Democrat Mark Fisher (1-5)",
    x = "Message Treatments"
  ) +
  # scale_fill_grey(start = 0.3, end = 0.7)+
  theme(legend.position = "none",
        axis.text = element_text(size = 6)) +
  facet_wrap(~LatLinkedFate.3pt, ncol = 5) +
  coord_cartesian(ylim = c(1, 5))

ggsave("Final/Appendix/study1_votemeans_bylinkedfate.jpg",
  width = 12,
  height = 6
)



## Immigrant generation

diag <- study1.cleaned.df %>%
  group_by(ImmGen) %>%
  summarize(count = n())

temp.1.df <- study1.cleaned.df %>%
  filter(ImmGen == 1)

temp.2.df <- study1.cleaned.df %>%
  filter(ImmGen == 2)

temp.3.df <- study1.cleaned.df %>%
  filter(ImmGen == 3)

## DV: likelihood of vote

ols3 <- lm(fishervote.5pt ~ Treatment,
  data = temp.3.df
)

ols_coef3 <- coeftest(ols3)

ols2 <- lm(fishervote.5pt ~ Treatment,
  data = temp.2.df
)

ols_coef2 <- coeftest(ols2)


ols1 <- lm(fishervote.5pt ~ Treatment,
  data = temp.1.df
)

ols_coef1 <- coeftest(ols1)


## Show treatment means

demcand.plot.df <- study1.cleaned.df %>%
  group_by(Treatment, ImmGen) %>%
  summarize(
    count = n(),
    fishervote = sum(fishervote.5pt, na.rm = TRUE) / count
  )


demcand.plot.df$Treatment <- factor(demcand.plot.df$Treatment,
  levels = c(
    "Control",
    "Pro-Undoc",
    "Progressive",
    "Race-Class"
  )
)


demcand.plot.df$se <- c(
  ols_coef1[1, 2],
  ols_coef2[1, 2],
  ols_coef3[1, 2],
  ols_coef1[2, 2],
  ols_coef2[2, 2],
  ols_coef3[2, 2],
  ols_coef1[3, 2],
  ols_coef2[3, 2],
  ols_coef3[3, 2],
  ols_coef1[4, 2],
  ols_coef2[4, 2],
  ols_coef3[4, 2]
)

ggplot(
  data = demcand.plot.df,
  aes(
    x = Treatment,
    y = fishervote,
    fill = Treatment
  )
) +
  geom_bar(stat = "identity") +
  geom_errorbar(
    aes(
      x = Treatment,
      ymin = fishervote - se,
      ymax = fishervote + se
    ),
    width = 0.2,
    position = position_dodge(.9),
    color = "black"
  ) +
  labs(
    y = "Hypothetical Vote for Democrat Mark Fisher (1-5)",
    x = "Message Treatments"
  ) +
  # scale_fill_grey(start = 0.3, end = 0.7)+
  theme(legend.position = "none") +
  facet_wrap(~ImmGen, ncol = 3) +
  coord_cartesian(ylim = c(1, 5))

ggsave("Final/Appendix/study1_votemeans_byimmgen.jpg",
  width = 10,
  height = 6
)





## National origin

diag <- study1.cleaned.df %>%
  group_by(NatlOrigin) %>%
  summarize(count = n())

study1.cleaned.df <- study1.cleaned.df %>%
  mutate(NatlOrigin.3pt = case_when(
    NatlOrigin == "Mexico" ~ "Mexico",
    NatlOrigin == "Puerto Rico" ~ "Puerto Rico",
    NatlOrigin %nin% c(
      "Mexico",
      "Puerto Rico"
    ) ~ "Other"
  ))

study1.cleaned.df$NatlOrigin.3pt <- factor(study1.cleaned.df$NatlOrigin.3pt,
  levels = c(
    "Mexico",
    "Puerto Rico",
    "Other"
  )
)

## Sample is heavily Mexican origin

temp.1.df <- study1.cleaned.df %>%
  filter(NatlOrigin == "Mexico")

temp.2.df <- study1.cleaned.df %>%
  filter(NatlOrigin == "Puerto Rico")

temp.3.df <- study1.cleaned.df %>%
  filter(NatlOrigin %nin% (c(
    "Mexico",
    "Puerto Rico"
  )))



ols3 <- lm(fishervote.5pt ~ Treatment,
  data = temp.3.df
)

ols_coef3 <- coeftest(ols3)

ols2 <- lm(fishervote.5pt ~ Treatment,
  data = temp.2.df
)

ols_coef2 <- coeftest(ols2)


ols1 <- lm(fishervote.5pt ~ Treatment,
  data = temp.1.df
)

ols_coef1 <- coeftest(ols1)


## Show treatment means

demcand.plot.df <- study1.cleaned.df %>%
  group_by(Treatment, NatlOrigin.3pt) %>%
  summarize(
    count = n(),
    fishervote = sum(fishervote.5pt, na.rm = TRUE) / count
  )


demcand.plot.df$Treatment <- factor(demcand.plot.df$Treatment,
  levels = c(
    "Control",
    "Pro-Undoc",
    "Progressive",
    "Race-Class"
  )
)


demcand.plot.df$se <- c(
  ols_coef1[1, 2],
  ols_coef2[1, 2],
  ols_coef3[1, 2],
  ols_coef1[2, 2],
  ols_coef2[2, 2],
  ols_coef3[2, 2],
  ols_coef1[3, 2],
  ols_coef2[3, 2],
  ols_coef3[3, 2],
  ols_coef1[4, 2],
  ols_coef2[4, 2],
  ols_coef3[4, 2]
)

ggplot(
  data = demcand.plot.df,
  aes(
    x = Treatment,
    y = fishervote,
    fill = Treatment
  )
) +
  geom_bar(stat = "identity") +
  geom_errorbar(
    aes(
      x = Treatment,
      ymin = fishervote - se,
      ymax = fishervote + se
    ),
    width = 0.2,
    position = position_dodge(.9),
    color = "black"
  ) +
  labs(
    y = "Hypothetical Vote for Democrat Mark Fisher (1-5)",
    x = "Message Treatments"
  ) +
  # scale_fill_grey(start = 0.3, end = 0.7)+
  theme(legend.position = "none") +
  facet_wrap(~NatlOrigin.3pt, ncol = 3) +
  coord_cartesian(ylim = c(1, 5))

ggsave("Final/Appendix/study1_votemeans_bynatlorigin.jpg",
  width = 10,
  height = 6
)


## Undocumented social network

diag <- study1.cleaned.df %>%
  group_by(undoc_know) %>%
  summarize(count = n())

study1.cleaned.df <- study1.cleaned.df %>%
  mutate(undoc_know.3pt = case_when(
    undoc_know == "None" ~ "None",
    undoc_know == "A lot" ~ "A lot",
    undoc_know %in% c(
      "Some",
      "A few"
    ) ~ "Some/A few"
  ))

study1.cleaned.df$undoc_know.3pt <- factor(study1.cleaned.df$undoc_know.3pt,
  levels = c(
    "None",
    "Some/A few",
    "A lot"
  )
)


temp.1.df <- study1.cleaned.df %>%
  filter(undoc_know.3pt == "None")

temp.2.df <- study1.cleaned.df %>%
  filter(undoc_know.3pt == "Some/A few")

temp.3.df <- study1.cleaned.df %>%
  filter(undoc_know.3pt == "A lot")

## DV: likelihood of vote

ols3 <- lm(fishervote.5pt ~ Treatment,
  data = temp.3.df
)

ols_coef3 <- coeftest(ols3)

ols2 <- lm(fishervote.5pt ~ Treatment,
  data = temp.2.df
)

ols_coef2 <- coeftest(ols2)


ols1 <- lm(fishervote.5pt ~ Treatment,
  data = temp.1.df
)

ols_coef1 <- coeftest(ols1)


## Show treatment means

demcand.plot.df <- study1.cleaned.df %>%
  group_by(Treatment, undoc_know.3pt) %>%
  summarize(
    count = n(),
    fishervote = sum(fishervote.5pt, na.rm = TRUE) / count
  )


demcand.plot.df$Treatment <- factor(demcand.plot.df$Treatment,
  levels = c(
    "Control",
    "Pro-Undoc",
    "Progressive",
    "Race-Class"
  )
)


demcand.plot.df$se <- c(
  ols_coef1[1, 2],
  ols_coef2[1, 2],
  ols_coef3[1, 2],
  ols_coef1[2, 2],
  ols_coef2[2, 2],
  ols_coef3[2, 2],
  ols_coef1[3, 2],
  ols_coef2[3, 2],
  ols_coef3[3, 2],
  ols_coef1[4, 2],
  ols_coef2[4, 2],
  ols_coef3[4, 2]
)

ggplot(
  data = demcand.plot.df,
  aes(
    x = Treatment,
    y = fishervote,
    fill = Treatment
  )
) +
  geom_bar(stat = "identity") +
  geom_errorbar(
    aes(
      x = Treatment,
      ymin = fishervote - se,
      ymax = fishervote + se
    ),
    width = 0.2,
    position = position_dodge(.9),
    color = "black"
  ) +
  labs(
    y = "Hypothetical Vote for Democrat Mark Fisher (1-5)",
    x = "Message Treatments"
  ) +
  # scale_fill_grey(start = 0.3, end = 0.7)+
  theme(legend.position = "none") +
  facet_wrap(~undoc_know.3pt, ncol = 3) +
  coord_cartesian(ylim = c(1, 5))

ggsave("Final/Appendix/study1_votemeans_byundocknown.jpg",
  width = 10,
  height = 6
)
```

## Study 2

```{r Study 2 Data preparation}

## Formatting data so that first (control) and second (treatment) ad are together

## Ad 1 and Ad 2: Democratic ads

ad1.df <- study2.cleaned.df %>%
  dplyr::select(party_id_str,
    party_id,
    pid_7pt = pid_num,
    lf_num = lat_linked_fate_numeric,
    natlorigin_clean,
    imm_gen,
    undoc_know,
    spanish_skills,
    english_skills,
    race,
    dv_ad2 = dv_controldem,
    ResponseId
  )

ad1.df$t_2 <- rep("Control")

ad2.df <- study2.cleaned.df %>%
  dplyr::select(party_id_str,
    party_id,
    pid_7pt = pid_num,
    lf_num = lat_linked_fate_numeric,
    natlorigin_clean,
    imm_gen,
    spanish_skills,
    english_skills,
    undoc_know,
    race,
    dv_ad2,
    t_2,
    ResponseId
  )

dempooled.df <- rbind(
  ad1.df,
  ad2.df
)


## Fixes

## Create 5pt PID

dempooled.df <- dempooled.df %>%
  mutate(pid_5pt = ifelse(pid_7pt %in% c(1, 2),
    1,
    ifelse(pid_7pt %in% c(6, 7),
      5,
      ifelse(pid_7pt == 3,
        2,
        ifelse(pid_7pt == 4,
          3,
          ifelse(pid_7pt == 5,
            4,
            NA
          )
        )
      )
    )
  ))

## Creating 3pt party ID
dempooled.df <- dempooled.df %>%
  mutate(pid_3pt = ifelse(pid_7pt %in% c(6, 7),
    "Democrat",
    ifelse(pid_7pt %in% c(3, 4, 5),
      "Independent",
      ifelse(pid_7pt %in% c(1, 2),
        "Republican",
        NA
      )
    )
  ))

## Fix treatment name for graph

dempooled.df <- dempooled.df %>%
  mutate(t_2 = ifelse(t_2 == "U",
    "Pro-Undocumented",
    ifelse(t_2 == "E",
      "Moderate Econ.",
      ifelse(t_2 == "L",
        "Pro-Legal",
        ifelse(t_2 == "P",
          "Progressive Econ.",
          ifelse(t_2 == "Control",
            "Control",
            NA
          )
        )
      )
    )
  ))


## Fixing factor levels

dempooled.df$party_id <- factor(dempooled.df$party_id,
  levels = c(
    "Democrat",
    "Independent lean Dem",
    "Independent no lean",
    "Independent lean Rep",
    "Republican"
  )
)

dempooled.df$t_2 <- factor(dempooled.df$t_2,
  levels = c(
    "Control",
    "Moderate Econ.",
    "Progressive Econ.",
    "Pro-Legal",
    "Pro-Undocumented"
  )
)

## Fixing Latino linked fate to be 1-5

dempooled.df$lf_num <- dempooled.df$lf_num + 1



## Adding ordinal/categorical factor variable

dempooled.df <- dempooled.df %>%
  mutate(dv_ad2_ordchar = ifelse(dv_ad2 == 1,
    "Extremely unlikely",
    ifelse(dv_ad2 == 2,
      "Unlikely",
      ifelse(dv_ad2 == 3,
        "Neither likely nor unlikely",
        ifelse(dv_ad2 == 4,
          "Likely",
          ifelse(dv_ad2 == 5,
            "Extremely likely",
            NA
          )
        )
      )
    )
  ))

dempooled.df$dv_ad2_ord <- factor(dempooled.df$dv_ad2_ordchar,
  levels = c(
    "Extremely unlikely",
    "Unlikely",
    "Neither likely nor unlikely",
    "Likely",
    "Extremely likely"
  )
)





### Same for Republican candidate messaging
### Merge control DVs in, label "Control", ResponseId as cluster variable

ad6.df <- study2.cleaned.df %>%
  dplyr::select(party_id_str,
    party_id,
    pid_7pt = pid_num,
    lf_num = lat_linked_fate_numeric,
    natlorigin_clean,
    imm_gen,
    spanish_skills,
    english_skills,
    undoc_know,
    race,
    DemOrder,
    dv_ad7 = dv_controlrep,
    ResponseId
  )

ad6.df$t_7 <- rep("Control")

ad7.df <- study2.cleaned.df %>%
  dplyr::select(party_id_str,
    party_id,
    pid_7pt = pid_num,
    lf_num = lat_linked_fate_numeric,
    natlorigin_clean,
    imm_gen,
    spanish_skills,
    english_skills,
    undoc_know,
    DemOrder,
    race,
    dv_ad7,
    t_7,
    ResponseId
  )

reppooled.df <- rbind(
  ad6.df,
  ad7.df
)


## Creating 5pt party ID
reppooled.df <- reppooled.df %>%
  mutate(pid_5pt = ifelse(pid_7pt %in% c(1, 2),
    1,
    ifelse(pid_7pt %in% c(6, 7),
      5,
      ifelse(pid_7pt == 3,
        2,
        ifelse(pid_7pt == 4,
          3,
          ifelse(pid_7pt == 5,
            4,
            NA
          )
        )
      )
    )
  ))


## Creating 3pt party ID
reppooled.df <- reppooled.df %>%
  mutate(pid_3pt = ifelse(pid_7pt %in% c(6, 7),
    "Democrat",
    ifelse(pid_7pt %in% c(3, 4, 5),
      "Independent",
      ifelse(pid_7pt %in% c(1, 2),
        "Republican",
        NA
      )
    )
  ))

## Fixing treatment names for graph

reppooled.df <- reppooled.df %>%
  mutate(t_7 = ifelse(t_7 == "U",
    "Anti-Undocumented",
    ifelse(t_7 == "E",
      "Moderate Econ.",
      ifelse(t_7 == "L",
        "Pro-Legal",
        ifelse(t_7 == "P",
          "Pro-Business",
          ifelse(t_7 == "Control",
            "Control",
            NA
          )
        )
      )
    )
  ))


## Fixing factor levels

reppooled.df$party_id <- factor(reppooled.df$party_id,
  levels = c(
    "Democrat",
    "Independent lean Dem",
    "Independent no lean",
    "Independent lean Rep",
    "Republican"
  )
)

reppooled.df$t_7 <- factor(reppooled.df$t_7,
  levels = c(
    "Control",
    "Moderate Econ.",
    "Pro-Business",
    "Pro-Legal",
    "Anti-Undocumented"
  )
)


## Fixing Latino linked fate to be 1-5

reppooled.df$lf_num <- reppooled.df$lf_num + 1



## Adding ordinal/categorical factor variable

reppooled.df <- reppooled.df %>%
  mutate(dv_ad7_ordchar = ifelse(dv_ad7 == 1,
    "Extremely unlikely",
    ifelse(dv_ad7 == 2,
      "Unlikely",
      ifelse(dv_ad7 == 3,
        "Neither likely nor unlikely",
        ifelse(dv_ad7 == 4,
          "Likely",
          ifelse(dv_ad7 == 5,
            "Extremely likely",
            NA
          )
        )
      )
    )
  ))

reppooled.df$dv_ad7_ord <- factor(reppooled.df$dv_ad7_ordchar,
  levels = c(
    "Extremely unlikely",
    "Unlikely",
    "Neither likely nor unlikely",
    "Likely",
    "Extremely likely"
  )
)
```



```{r Study 2 Main Figures: Figure 2 Democratic Candidate}
## Estimate Effect of Ad 2 treatment on Ad evaluations, anchored by Ad 1
## Separate models for each PID subgroup

### Calculate OLS by PID
### Standard errors are re-calculated, clustered at respondent level

df1 <- dempooled.df %>% filter(party_id == "Democrat")

lm1 <- lm(dv_ad2 ~ t_2, data = df1)

lm1_coef <- coeftest(lm1,
  vcov = vcovCL(lm1, factor(df1$ResponseId))
)



df2 <- dempooled.df %>% filter(party_id == "Independent lean Dem")

lm2 <- lm(dv_ad2 ~ t_2, data = df2)

lm2_coef <- coeftest(lm2,
  vcov = vcovCL(lm2, factor(df2$ResponseId))
)



df3 <- dempooled.df %>% filter(party_id == "Independent no lean")

lm3 <- lm(dv_ad2 ~ t_2, data = df3)

lm3_coef <- coeftest(lm3,
  vcov = vcovCL(lm3, factor(df3$ResponseId))
)


df4 <- dempooled.df %>% filter(party_id == "Independent lean Rep")

lm4 <- lm(dv_ad2 ~ t_2, data = df4)

lm4_coef <- coeftest(lm4,
  vcov = vcovCL(lm4, factor(df4$ResponseId))
)

df5 <- dempooled.df %>% filter(party_id == "Republican")

lm5 <- lm(dv_ad2 ~ t_2, data = df5)

lm5_coef <- coeftest(lm5,
  vcov = vcovCL(lm5, factor(df5$ResponseId))
)

stargazer(lm1, lm2, lm3, lm4, lm5,
  se = list(lm1_coef[, 2], lm2_coef[, 2], lm3_coef[, 2], lm4_coef[, 2], lm5_coef[, 2]),
  type = "latex",
  out = "Final/demcand_ads1_2_bypid_ols.tex",
  column.labels = c(
    "Dems",
    "Ind-Dem",
    "Ind",
    "Ind-Rep",
    "Reps"
  ),
  dep.var.caption = "DV: Sentiment towards Ad (1-5)",
  dep.var.labels = "Subgroups: Individual Party ID/Lean",
  covariate.labels = c(
    "Moderate Econ.",
    "Progressive Econ.",
    "Pro-Legal",
    "Pro-Undocumented"
  )
)



## Making figure

df1 <- tibble(
  Message = c(
    "Intercept",
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = lm1_coef[, 1],
  se = lm1_coef[, 2],
  pval = lm1_coef[, 4],
  pid = c(rep("Democrat"))
)

df2 <- tibble(
  Message = c(
    "Intercept",
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = lm2_coef[, 1],
  se = lm2_coef[, 2],
  pval = lm2_coef[, 4],
  pid = c(rep("Ind-Lean Dem"))
)

df3 <- tibble(
  Message = c(
    "Intercept",
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = lm3_coef[, 1],
  se = lm3_coef[, 2],
  pval = lm3_coef[, 4],
  pid = c(rep("Ind-No lean"))
)


df4 <- tibble(
  Message = c(
    "Intercept",
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = lm4_coef[, 1],
  se = lm4_coef[, 2],
  pval = lm4_coef[, 4],
  pid = c(rep("Ind-Lean Rep"))
)

df5 <- tibble(
  Message = c(
    "Intercept",
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = lm5_coef[, 1],
  se = lm5_coef[, 2],
  pval = lm5_coef[, 4],
  pid = c(rep("Republican"))
)


demcand_df <- rbind(df1, df2, df3, df4, df5)


## Cleaning and final fixes

demcand_df <- demcand_df %>%
  filter(Message != "Intercept")

demcand_df$pid <- factor(demcand_df$pid,
  levels = c(
    "Democrat",
    "Ind-Lean Dem",
    "Ind-No lean",
    "Ind-Lean Rep",
    "Republican"
  )
)

demcand_df$Message <- factor(demcand_df$Message,
  levels = c(
    "Pro-Undocumented (D)",
    "Pro-Legal (D)",
    "Progressive Econ. (D)",
    "Moderate Econ. (D)"
  )
)



## Graph

demcand_plot <- ggplot(
  data = demcand_df,
  aes(
    x = Message,
    y = coefficient
  )
) +
  geom_point(
    aes(
      x = Message,
      y = coefficient,
      color = pid
    ),
    size = 4
  ) +
  geom_linerange(
    aes(
      x = Message,
      color = pid,
      ymin = coefficient - 1.96 * se,
      ymax = coefficient + 1.96 * se
    ),
    lwd = 2
  ) +
  scale_color_manual(values = c(
    "blue2",
    "green4",
    "yellow4",
    "orange2",
    "red4"
  )) +
  facet_wrap(~pid, ncol = 5) +
  coord_flip() +
  labs(
    x = "",
    y = "Effect of Treatment Ad Assignment on Ad Evaluation (1-5)"
  ) +
  theme(
    legend.position = "none",
    text = element_text(size = 18)
  ) +
  ylim(-1.1, 0.6)


demcand_plot + geom_hline(
  data = demcand_df,
  aes(yintercept = 0)
)

ggsave("Final/study2_demcandfigure.png",
  height = 6,
  width = 12
)
```



```{r Study 2 Main Figures: Figure 3 Republican Candidate}
## Estimate Effect of Ad 2 treatment on Ad evaluations, anchored by Ad 1
## Separate models for each PID subgroup

### Calculate OLS by PID
### Standard errors are re-calculated, clustered at respondent level

df1 <- reppooled.df %>% filter(party_id == "Democrat")

lm1 <- lm(dv_ad7 ~ t_7, data = df1)

lm1_coef <- coeftest(lm1,
  vcov = vcovCL(lm1, factor(df1$ResponseId))
)



df2 <- reppooled.df %>% filter(party_id == "Independent lean Dem")

lm2 <- lm(dv_ad7 ~ t_7, data = df2)

lm2_coef <- coeftest(lm2,
  vcov = vcovCL(lm2, factor(df2$ResponseId))
)



df3 <- reppooled.df %>% filter(party_id == "Independent no lean")

lm3 <- lm(dv_ad7 ~ t_7, data = df3)

lm3_coef <- coeftest(lm3,
  vcov = vcovCL(lm3, factor(df3$ResponseId))
)


df4 <- reppooled.df %>% filter(party_id == "Independent lean Rep")

lm4 <- lm(dv_ad7 ~ t_7, data = df4)

lm4_coef <- coeftest(lm4,
  vcov = vcovCL(lm4, factor(df4$ResponseId))
)

df5 <- reppooled.df %>% filter(party_id == "Republican")

lm5 <- lm(dv_ad7 ~ t_7, data = df5)

lm5_coef <- coeftest(lm5,
  vcov = vcovCL(lm5, factor(df5$ResponseId))
)

stargazer(lm1, lm2, lm3, lm4, lm5,
  se = list(lm1_coef[, 2], lm2_coef[, 2], lm3_coef[, 2], lm4_coef[, 2], lm5_coef[, 2]),
  type = "latex",
  out = "Final/repcand_ads1_2_bypid_ols.tex",
  column.labels = c(
    "Dems",
    "Ind-Dem",
    "Ind",
    "Ind-Rep",
    "Reps"
  ),
  dep.var.caption = "DV: Sentiment towards Ad (1-5)",
  dep.var.labels = "Subgroups: Individual Party ID/Lean",
  covariate.labels = c(
    "Moderate Econ.",
    "Pro Business",
    "Pro-Legal",
    "Anti-Undocumented"
  )
)



## Making figure

df1 <- tibble(
  Message = c(
    "Intercept",
    "Moderate Econ. (R)",
    "Pro-Business (R)",
    "Pro-Legal (R)",
    "Anti-Undocumented (R)"
  ),
  coefficient = lm1_coef[, 1],
  se = lm1_coef[, 2],
  pid = c(rep("Democrat"))
)

df2 <- tibble(
  Message = c(
    "Intercept",
    "Moderate Econ. (R)",
    "Pro-Business (R)",
    "Pro-Legal (R)",
    "Anti-Undocumented (R)"
  ),
  coefficient = lm2_coef[, 1],
  se = lm2_coef[, 2],
  pid = c(rep("Ind-Lean Dem"))
)

df3 <- tibble(
  Message = c(
    "Intercept",
    "Moderate Econ. (R)",
    "Pro-Business (R)",
    "Pro-Legal (R)",
    "Anti-Undocumented (R)"
  ),
  coefficient = lm3_coef[, 1],
  se = lm3_coef[, 2],
  pid = c(rep("Ind-No lean"))
)


df4 <- tibble(
  Message = c(
    "Intercept",
    "Moderate Econ. (R)",
    "Pro-Business (R)",
    "Pro-Legal (R)",
    "Anti-Undocumented (R)"
  ),
  coefficient = lm4_coef[, 1],
  se = lm4_coef[, 2],
  pid = c(rep("Ind-Lean Rep"))
)

df5 <- tibble(
  Message = c(
    "Intercept",
    "Moderate Econ. (R)",
    "Pro-Business (R)",
    "Pro-Legal (R)",
    "Anti-Undocumented (R)"
  ),
  coefficient = lm5_coef[, 1],
  se = lm5_coef[, 2],
  pid = c(rep("Republican"))
)


repcand_df <- rbind(df1, df2, df3, df4, df5)


## Cleaning and final fixes

repcand_df <- repcand_df %>%
  filter(Message != "Intercept")

repcand_df$pid <- factor(repcand_df$pid,
  levels = c(
    "Democrat",
    "Ind-Lean Dem",
    "Ind-No lean",
    "Ind-Lean Rep",
    "Republican"
  )
)

repcand_df$Message <- factor(repcand_df$Message,
  levels = c(
    "Anti-Undocumented (R)",
    "Pro-Legal (R)",
    "Pro-Business (R)",
    "Moderate Econ. (R)"
  )
)


## Graph

repcand_plot <- ggplot(
  data = repcand_df,
  aes(
    x = Message,
    y = coefficient
  )
) +
  geom_point(
    aes(
      x = Message,
      y = coefficient,
      color = pid
    ),
    size = 4
  ) +
  geom_linerange(
    aes(
      x = Message,
      color = pid,
      ymin = coefficient - 1.96 * se,
      ymax = coefficient + 1.96 * se
    ),
    lwd = 2
  ) +
  scale_color_manual(values = c(
    "blue2",
    "green4",
    "yellow4",
    "orange2",
    "red4"
  )) +
  facet_wrap(~pid, ncol = 5) +
  coord_flip() +
  labs(
    x = "",
    y = "Effect of Treatment Ad Assignment on Ad Evaluation (1-5)"
  ) +
  theme(
    legend.position = "none",
    text = element_text(size = 18)
  ) +
  ylim(-1, 0.6)


repcand_plot + geom_hline(
  data = repcand_df,
  aes(yintercept = 0)
)


ggsave("Final/study2_repcandfigure.png",
  height = 6,
  width = 12
)
```



```{r Study 2 Appendix: 3pt PID}


## Group independents with partisans

## Estimate Effect of Ad 2 treatment on Ad evaluations, anchored by Ad 1
## Separate models for each PID subgroup

### Calculate OLS by PID
### Standard errors are re-calculated, clustered at respondent level

df1 <- dempooled.df %>% filter(party_id %in% c("Democrat", "Independent lean Dem"))

lm1 <- lm(dv_ad2 ~ t_2, data = df1)

lm1_coef <- coeftest(lm1,
  vcov = vcovCL(lm1, factor(df1$ResponseId))
)



df3 <- dempooled.df %>% filter(party_id == "Independent no lean")

lm3 <- lm(dv_ad2 ~ t_2, data = df3)

lm3_coef <- coeftest(lm3,
  vcov = vcovCL(lm3, factor(df3$ResponseId))
)


df5 <- dempooled.df %>% filter(party_id %in% c("Republican", "Independent lean Rep"))

lm5 <- lm(dv_ad2 ~ t_2, data = df5)

lm5_coef <- coeftest(lm5,
  vcov = vcovCL(lm5, factor(df5$ResponseId))
)

stargazer(lm1, lm3, lm5,
  se = list(lm1_coef[, 2], lm3_coef[, 2], lm5_coef[, 2]),
  type = "latex",
  out = "Final/Appendix/demcand_ads1_2_by3ptpid_leanersaspartisans_ols.tex",
  column.labels = c(
    "Dems/Lean Dems",
    "True Ind",
    "Reps/Lean Reps"
  ),
  dep.var.caption = "DV: Sentiment towards Ad (1-5)",
  dep.var.labels = "Subgroups: Individual Party ID/Lean",
  covariate.labels = c(
    "Moderate Econ.",
    "Progressive Econ.",
    "Pro-Legal",
    "Pro-Undocumented"
  )
)



## Making figure

df1 <- tibble(
  Message = c(
    "Intercept",
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = lm1_coef[, 1],
  se = lm1_coef[, 2],
  pval = lm1_coef[, 4],
  pid = c(rep("Democrat/Dem Leaner"))
)

df3 <- tibble(
  Message = c(
    "Intercept",
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = lm3_coef[, 1],
  se = lm3_coef[, 2],
  pval = lm3_coef[, 4],
  pid = c(rep("True Independent"))
)

df5 <- tibble(
  Message = c(
    "Intercept",
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = lm5_coef[, 1],
  se = lm5_coef[, 2],
  pval = lm5_coef[, 4],
  pid = c(rep("Republican/Rep Leaner"))
)


demcand_df <- rbind(df1, df3, df5)


## Cleaning and final fixes

demcand_df <- demcand_df %>%
  filter(Message != "Intercept")

demcand_df$pid <- factor(demcand_df$pid,
  levels = c(
    "Democrat/Dem Leaner",
    "True Independent",
    "Republican/Rep Leaner"
  )
)

demcand_df$Message <- factor(demcand_df$Message,
  levels = c(
    "Pro-Undocumented (D)",
    "Pro-Legal (D)",
    "Progressive Econ. (D)",
    "Moderate Econ. (D)"
  )
)



## Graph

demcand_plot <- ggplot(
  data = demcand_df,
  aes(
    x = Message,
    y = coefficient
  )
) +
  geom_point(
    aes(
      x = Message,
      y = coefficient,
      color = pid
    ),
    size = 4
  ) +
  geom_linerange(
    aes(
      x = Message,
      color = pid,
      ymin = coefficient - 1.96 * se,
      ymax = coefficient + 1.96 * se
    ),
    lwd = 2
  ) +
  scale_color_manual(values = c(
    "blue2",
    "yellow4",
    "red4"
  )) +
  facet_wrap(~pid, ncol = 3) +
  coord_flip() +
  labs(
    x = "",
    y = "Effect of Treatment Ad Assignment on Ad Evaluation (1-5)"
  ) +
  theme(
    legend.position = "none",
    text = element_text(size = 18)
  ) +
  ylim(-1.1, 0.6)


demcand_plot + geom_hline(
  data = demcand_df,
  aes(yintercept = 0)
)

ggsave("Final/Appendix/study2_demcandfigure_3ptpid.png",
  height = 6,
  width = 12
)



## Republican candidate

## Estimate Effect of Ad 2 treatment on Ad evaluations, anchored by Ad 1
## Separate models for each PID subgroup

### Calculate OLS by PID
### Standard errors are re-calculated, clustered at respondent level

df1 <- reppooled.df %>% filter(party_id %in% c("Democrat", "Independent lean Dem"))

lm1 <- lm(dv_ad7 ~ t_7, data = df1)

lm1_coef <- coeftest(lm1,
  vcov = vcovCL(lm1, factor(df1$ResponseId))
)



df3 <- reppooled.df %>% filter(party_id == "Independent no lean")

lm3 <- lm(dv_ad7 ~ t_7, data = df3)

lm3_coef <- coeftest(lm3,
  vcov = vcovCL(lm3, factor(df3$ResponseId))
)


df5 <- reppooled.df %>% filter(party_id %in% c("Republican", "Independent lean Rep"))

lm5 <- lm(dv_ad7 ~ t_7, data = df5)

lm5_coef <- coeftest(lm5,
  vcov = vcovCL(lm5, factor(df5$ResponseId))
)

stargazer(lm1, lm3, lm5,
  se = list(lm1_coef[, 2], lm3_coef[, 2], lm5_coef[, 2]),
  type = "latex",
  out = "Final/Appendix/repcand_ads1_2_by3ptpid_ols.tex",
  column.labels = c(
    "Dems/Lean Dem",
    "True Ind",
    "Reps/Lean Rep"
  ),
  dep.var.caption = "DV: Sentiment towards Ad (1-5)",
  dep.var.labels = "Subgroups: Individual Party ID/Lean",
  covariate.labels = c(
    "Moderate Econ.",
    "Pro Business",
    "Pro-Legal",
    "Anti-Undocumented"
  )
)



## Making figure

df1 <- tibble(
  Message = c(
    "Intercept",
    "Moderate Econ. (R)",
    "Pro-Business (R)",
    "Pro-Legal (R)",
    "Anti-Undocumented (R)"
  ),
  coefficient = lm1_coef[, 1],
  se = lm1_coef[, 2],
  pid = c(rep("Democrat/Lean Dem"))
)


df3 <- tibble(
  Message = c(
    "Intercept",
    "Moderate Econ. (R)",
    "Pro-Business (R)",
    "Pro-Legal (R)",
    "Anti-Undocumented (R)"
  ),
  coefficient = lm3_coef[, 1],
  se = lm3_coef[, 2],
  pid = c(rep("True Independent"))
)




df5 <- tibble(
  Message = c(
    "Intercept",
    "Moderate Econ. (R)",
    "Pro-Business (R)",
    "Pro-Legal (R)",
    "Anti-Undocumented (R)"
  ),
  coefficient = lm5_coef[, 1],
  se = lm5_coef[, 2],
  pid = c(rep("Republican/Lean Rep"))
)


repcand_df <- rbind(df1, df3,  df5)


## Cleaning and final fixes

repcand_df <- repcand_df %>%
  filter(Message != "Intercept")

repcand_df$pid <- factor(repcand_df$pid,
  levels = c(
    "Democrat/Lean Dem",
    "True Independent",
    "Republican/Lean Rep"
  )
)

repcand_df$Message <- factor(repcand_df$Message,
  levels = c(
    "Anti-Undocumented (R)",
    "Pro-Legal (R)",
    "Pro-Business (R)",
    "Moderate Econ. (R)"
  )
)


## Graph

repcand_plot <- ggplot(
  data = repcand_df,
  aes(
    x = Message,
    y = coefficient
  )
) +
  geom_point(
    aes(
      x = Message,
      y = coefficient,
      color = pid
    ),
    size = 4
  ) +
  geom_linerange(
    aes(
      x = Message,
      color = pid,
      ymin = coefficient - 1.96 * se,
      ymax = coefficient + 1.96 * se
    ),
    lwd = 2
  ) +
  scale_color_manual(values = c(
    "blue2",
    "yellow4",
    "red4"
  )) +
  facet_wrap(~pid, ncol = 3) +
  coord_flip() +
  labs(
    x = "",
    y = "Effect of Treatment Ad Assignment on Ad Evaluation (1-5)"
  ) +
  theme(
    legend.position = "none",
    text = element_text(size = 18)
  ) +
  ylim(-1, 0.6)


repcand_plot + geom_hline(
  data = repcand_df,
  aes(yintercept = 0)
)


ggsave("Final/Appendix/study2_repcandfigure_3ptpid.png",
  height = 6,
  width = 12)


```



```{r Study 2 Appendix: 7pt PID}


diag_df <- dempooled.df %>%
  group_by(party_id_str,
           pid_7pt) %>%
  reframe(count=n())

## 7 point PID

## Estimate Effect of Ad 2 treatment on Ad evaluations, anchored by Ad 1
## Separate models for each PID subgroup

### Calculate OLS by PID
### Standard errors are re-calculated, clustered at respondent level

df1 <- dempooled.df %>% filter(pid_7pt == 7)

lm1 <- lm(dv_ad2 ~ t_2, data = df1)

lm1_coef <- coeftest(lm1,
  vcov = vcovCL(lm1, factor(df1$ResponseId))
)


df2 <- dempooled.df %>% filter(pid_7pt == 6)

lm2 <- lm(dv_ad2 ~ t_2, data = df2)

lm2_coef <- coeftest(lm2,
  vcov = vcovCL(lm2, factor(df2$ResponseId))
)


df3 <- dempooled.df %>% filter(pid_7pt == 5)

lm3 <- lm(dv_ad2 ~ t_2, data = df3)

lm3_coef <- coeftest(lm3,
  vcov = vcovCL(lm3, factor(df3$ResponseId))
)

df4 <- dempooled.df %>% filter(pid_7pt == 4)

lm4 <- lm(dv_ad2 ~ t_2, data = df4)

lm4_coef <- coeftest(lm4,
  vcov = vcovCL(lm4, factor(df4$ResponseId))
)

df5 <- dempooled.df %>% filter(pid_7pt == 3)

lm5 <- lm(dv_ad2 ~ t_2, data = df5)

lm5_coef <- coeftest(lm5,
  vcov = vcovCL(lm5, factor(df5$ResponseId))
)

df6 <- dempooled.df %>% filter(pid_7pt == 2)

lm6 <- lm(dv_ad2 ~ t_2, data = df6)

lm6_coef <- coeftest(lm6,
  vcov = vcovCL(lm6, factor(df6$ResponseId))
)

df7 <- dempooled.df %>% filter(pid_7pt == 1)

lm7 <- lm(dv_ad2 ~ t_2, data = df7)

lm7_coef <- coeftest(lm7,
  vcov = vcovCL(lm7, factor(df7$ResponseId))
)



stargazer(lm1, lm2, lm3, lm4, lm5, lm6, lm7,
  se = list(lm1_coef[, 2], 
            lm2_coef[, 2], 
            lm3_coef[, 2], 
            lm4_coef[, 2],
            lm5_coef[, 2],
            lm6_coef[, 2],
            lm7_coef[, 2]),
  type = "latex",
  out = "Final/Appendix/demcand_ads1_2_by7ptpid_leanersaspartisans_ols.tex",
  dep.var.caption = "DV: Sentiment towards Ad (1-5)",
  dep.var.labels = "Subgroups: Individual Party ID/Lean",
  covariate.labels = c(
    "Moderate Econ.",
    "Progressive Econ.",
    "Pro-Legal",
    "Pro-Undocumented"
  )
)



## Making figure

df1 <- tibble(
  Message = c(
    "Intercept",
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = lm1_coef[, 1],
  se = lm1_coef[, 2],
  pval = lm1_coef[, 4],
  pid = c(rep("Strong Dem"))
)


df2 <- tibble(
  Message = c(
    "Intercept",
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = lm2_coef[, 1],
  se = lm2_coef[, 2],
  pval = lm2_coef[, 4],
  pid = c(rep("Weak Dem"))
)

df3 <- tibble(
  Message = c(
    "Intercept",
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = lm3_coef[, 1],
  se = lm3_coef[, 2],
  pval = lm3_coef[, 4],
  pid = c(rep("Ind lean Dem"))
)

df4 <- tibble(
  Message = c(
    "Intercept",
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = lm4_coef[, 1],
  se = lm4_coef[, 2],
  pval = lm4_coef[, 4],
  pid = c(rep("True Ind"))
)

df5 <- tibble(
  Message = c(
    "Intercept",
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = lm5_coef[, 1],
  se = lm5_coef[, 2],
  pval = lm5_coef[, 4],
  pid = c(rep("Ind lean Rep"))
)

df6 <- tibble(
  Message = c(
    "Intercept",
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = lm6_coef[, 1],
  se = lm6_coef[, 2],
  pval = lm6_coef[, 4],
  pid = c(rep("Weak Rep"))
)

df7 <- tibble(
  Message = c(
    "Intercept",
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = lm7_coef[, 1],
  se = lm7_coef[, 2],
  pval = lm7_coef[, 4],
  pid = c(rep("Strong Rep"))
)



demcand_df <- rbind(df1, df2, df3, df4, df5, df6, df7)


## Cleaning and final fixes

demcand_df <- demcand_df %>%
  filter(Message != "Intercept")

demcand_df$pid <- factor(demcand_df$pid,
  levels = c(
    "Strong Dem",
    "Weak Dem",
    "Ind lean Dem",
    "True Ind",
    "Ind lean Rep",
    "Weak Rep",
    "Strong Rep"
  )
)

demcand_df$Message <- factor(demcand_df$Message,
  levels = c(
    "Pro-Undocumented (D)",
    "Pro-Legal (D)",
    "Progressive Econ. (D)",
    "Moderate Econ. (D)"
  )
)



## Graph

demcand_plot <- ggplot(
  data = demcand_df,
  aes(
    x = Message,
    y = coefficient
  )
) +
  geom_point(
    aes(
      x = Message,
      y = coefficient,
      color = pid
    ),
    size = 4
  ) +
  geom_linerange(
    aes(
      x = Message,
      color = pid,
      ymin = coefficient - 1.96 * se,
      ymax = coefficient + 1.96 * se
    ),
    lwd = 2)+
  facet_wrap(~pid, ncol = 7) +
  coord_flip() +
  labs(
    x = "",
    y = "Effect of Treatment Ad Assignment on Ad Evaluation (1-5)"
  ) +
  theme(
    legend.position = "none",
    text = element_text(size = 18)
  ) +
  ylim(-1.1, 0.6)


demcand_plot + geom_hline(
  data = demcand_df,
  aes(yintercept = 0)
)

ggsave("Final/Appendix/study2_demcandfigure_7ptpid.png",
  height = 6,
  width = 12
)



## Republican candidate

## Estimate Effect of Ad 2 treatment on Ad evaluations, anchored by Ad 1
## Separate models for each PID subgroup

### Calculate OLS by PID
### Standard errors are re-calculated, clustered at respondent level

df1 <- reppooled.df %>% filter(pid_7pt == 7)

lm1 <- lm(dv_ad7 ~ t_7, data = df1)

lm1_coef <- coeftest(lm1,
  vcov = vcovCL(lm1, factor(df1$ResponseId))
)


df2 <- reppooled.df %>% filter(pid_7pt == 6)

lm2 <- lm(dv_ad7 ~ t_7, data = df2)

lm2_coef <- coeftest(lm2,
  vcov = vcovCL(lm2, factor(df2$ResponseId))
)


df3 <- reppooled.df %>% filter(pid_7pt == 5)

lm3 <- lm(dv_ad7 ~ t_7, data = df3)

lm3_coef <- coeftest(lm3,
  vcov = vcovCL(lm3, factor(df3$ResponseId))
)

df4 <- reppooled.df %>% filter(pid_7pt == 4)

lm4 <- lm(dv_ad7 ~ t_7, data = df4)

lm4_coef <- coeftest(lm4,
  vcov = vcovCL(lm4, factor(df4$ResponseId))
)

df5 <- reppooled.df %>% filter(pid_7pt == 3)

lm5 <- lm(dv_ad7 ~ t_7, data = df5)

lm5_coef <- coeftest(lm5,
  vcov = vcovCL(lm5, factor(df5$ResponseId))
)

df6 <- reppooled.df %>% filter(pid_7pt == 2)

lm6 <- lm(dv_ad7 ~ t_7, data = df6)

lm6_coef <- coeftest(lm6,
  vcov = vcovCL(lm6, factor(df6$ResponseId))
)

df7 <- reppooled.df %>% filter(pid_7pt == 1)

lm7 <- lm(dv_ad7 ~ t_7, data = df7)

lm7_coef <- coeftest(lm7,
  vcov = vcovCL(lm7, factor(df7$ResponseId))
)


stargazer(lm1, lm2, lm3, lm4, lm5, lm6, lm7,
  se = list(lm1_coef[, 2], 
            lm2_coef[, 2], 
            lm3_coef[, 2], 
            lm4_coef[, 2], 
            lm5_coef[, 2],
            lm6_coef[, 2],
            lm7_coef[, 2]),
  type = "latex",
  out = "Final/Appendix/repcand_ads1_2_by7ptpid_ols.tex",
  dep.var.caption = "DV: Sentiment towards Ad (1-5)",
  dep.var.labels = "Subgroups: Individual Party ID/Lean",
  covariate.labels = c(
    "Moderate Econ.",
    "Pro Business",
    "Pro-Legal",
    "Anti-Undocumented"
  )
)



## Making figure

df1 <- tibble(
  Message = c(
    "Intercept",
    "Moderate Econ. (R)",
    "Pro-Business (R)",
    "Pro-Legal (R)",
    "Anti-Undocumented (R)"
  ),
  coefficient = lm1_coef[, 1],
  se = lm1_coef[, 2],
  pid = c(rep("Strong Dem"))
)

df2 <- tibble(
  Message = c(
    "Intercept",
    "Moderate Econ. (R)",
    "Pro-Business (R)",
    "Pro-Legal (R)",
    "Anti-Undocumented (R)"
  ),
  coefficient = lm2_coef[, 1],
  se = lm2_coef[, 2],
  pid = c(rep("Weak Dem"))
)


df3 <- tibble(
  Message = c(
    "Intercept",
    "Moderate Econ. (R)",
    "Pro-Business (R)",
    "Pro-Legal (R)",
    "Anti-Undocumented (R)"
  ),
  coefficient = lm3_coef[, 1],
  se = lm3_coef[, 2],
  pid = c(rep("Ind lean Dem"))
)

df4 <- tibble(
  Message = c(
    "Intercept",
    "Moderate Econ. (R)",
    "Pro-Business (R)",
    "Pro-Legal (R)",
    "Anti-Undocumented (R)"
  ),
  coefficient = lm4_coef[, 1],
  se = lm4_coef[, 2],
  pid = c(rep("True Ind"))
)


df5 <- tibble(
  Message = c(
    "Intercept",
    "Moderate Econ. (R)",
    "Pro-Business (R)",
    "Pro-Legal (R)",
    "Anti-Undocumented (R)"
  ),
  coefficient = lm5_coef[, 1],
  se = lm5_coef[, 2],
  pid = c(rep("Ind lean Rep"))
)

df6 <- tibble(
  Message = c(
    "Intercept",
    "Moderate Econ. (R)",
    "Pro-Business (R)",
    "Pro-Legal (R)",
    "Anti-Undocumented (R)"
  ),
  coefficient = lm6_coef[, 1],
  se = lm6_coef[, 2],
  pid = c(rep("Weak Rep"))
)

df7 <- tibble(
  Message = c(
    "Intercept",
    "Moderate Econ. (R)",
    "Pro-Business (R)",
    "Pro-Legal (R)",
    "Anti-Undocumented (R)"
  ),
  coefficient = lm7_coef[, 1],
  se = lm7_coef[, 2],
  pid = c(rep("Strong Rep"))
)


repcand_df <- rbind(df1, df2, df3, df4, df5, df6, df7)


## Cleaning and final fixes

repcand_df <- repcand_df %>%
  filter(Message != "Intercept")

repcand_df$pid <- factor(repcand_df$pid,
  levels = c(
    "Strong Dem",
    "Weak Dem",
    "Ind lean Dem",
    "True Ind",
    "Ind lean Rep",
    "Weak Rep",
    "Strong Rep"
  )
)

repcand_df$Message <- factor(repcand_df$Message,
  levels = c(
    "Anti-Undocumented (R)",
    "Pro-Legal (R)",
    "Pro-Business (R)",
    "Moderate Econ. (R)"
  )
)


## Graph

repcand_plot <- ggplot(
  data = repcand_df,
  aes(
    x = Message,
    y = coefficient
  )
) +
  geom_point(
    aes(
      x = Message,
      y = coefficient,
      color = pid
    ),
    size = 4
  ) +
  geom_linerange(
    aes(
      x = Message,
      color = pid,
      ymin = coefficient - 1.96 * se,
      ymax = coefficient + 1.96 * se
    ),
    lwd = 2
  ) +
  facet_wrap(~pid, ncol = 7) +
  coord_flip() +
  labs(
    x = "",
    y = "Effect of Treatment Ad Assignment on Ad Evaluation (1-5)"
  ) +
  theme(
    legend.position = "none",
    text = element_text(size = 18)
  )

repcand_plot + geom_hline(
  data = repcand_df,
  aes(yintercept = 0)
)


ggsave("Final/Appendix/study2_repcandfigure_7ptpid.png",
  height = 6,
  width = 12)


```


```{r Study 2 Appendix: All Outcome Variables Figure}
## Democratic candidate

## Using all outcome variables

ad1.df <- study2.cleaned.df %>%
  dplyr::select(party_id_str,
    party_id,
    pid_7pt = pid_num,
    lf_num = lat_linked_fate_numeric,
    natlorigin_clean,
    imm_gen,
    undoc_know,
    dv_ad = dv_controldem,
    ResponseId
  )

ad1.df$Treatment <- c(rep("Control"))

ad2.df <- study2.cleaned.df %>%
  dplyr::select(party_id_str,
    party_id,
    pid_7pt = pid_num,
    lf_num = lat_linked_fate_numeric,
    natlorigin_clean,
    imm_gen,
    undoc_know,
    dv_ad = dv_ad2,
    Treatment = t_2,
    ResponseId
  )

ad3.df <- study2.cleaned.df %>%
  dplyr::select(party_id_str,
    party_id,
    pid_7pt = pid_num,
    lf_num = lat_linked_fate_numeric,
    natlorigin_clean,
    imm_gen,
    undoc_know,
    dv_ad = dv_ad3,
    Treatment = t_3,
    ResponseId
  )

ad4.df <- study2.cleaned.df %>%
  dplyr::select(party_id_str,
    party_id,
    pid_7pt = pid_num,
    lf_num = lat_linked_fate_numeric,
    natlorigin_clean,
    imm_gen,
    undoc_know,
    dv_ad = dv_ad4,
    Treatment = t_4,
    ResponseId
  )

ad5.df <- study2.cleaned.df %>%
  dplyr::select(party_id_str,
    party_id,
    pid_7pt = pid_num,
    lf_num = lat_linked_fate_numeric,
    natlorigin_clean,
    imm_gen,
    undoc_know,
    dv_ad = dv_ad5,
    Treatment = t_5,
    ResponseId
  )


demads_df <- rbind(
  ad1.df,
  ad2.df,
  ad3.df,
  ad4.df,
  ad5.df
)


### Calculate OLS by PID
### Standard errors are re-calculated, clustered at respondent level

df1 <- demads_df %>% filter(party_id == "Democrat")

lm1 <- lm(dv_ad ~ Treatment, data = df1)

lm1_coef <- coeftest(lm1,
  vcov = vcovCL(lm1, factor(df1$ResponseId))
)



df2 <- demads_df %>% filter(party_id == "Independent lean Dem")

lm2 <- lm(dv_ad ~ Treatment, data = df2)

lm2_coef <- coeftest(lm2,
  vcov = vcovCL(lm2, factor(df2$ResponseId))
)



df3 <- demads_df %>% filter(party_id == "Independent no lean")

lm3 <- lm(dv_ad ~ Treatment, data = df3)

lm3_coef <- coeftest(lm3,
  vcov = vcovCL(lm3, factor(df3$ResponseId))
)


df4 <- demads_df %>% filter(party_id == "Independent lean Rep")

lm4 <- lm(dv_ad ~ Treatment, data = df4)

lm4_coef <- coeftest(lm4,
  vcov = vcovCL(lm4, factor(df4$ResponseId))
)

df5 <- demads_df %>% filter(party_id == "Republican")

lm5 <- lm(dv_ad ~ Treatment, data = df5)

lm5_coef <- coeftest(lm5,
  vcov = vcovCL(lm5, factor(df5$ResponseId))
)


stargazer(lm1, lm2, lm3, lm4, lm5,
  se = list(lm1_coef[, 2], lm2_coef[, 2], lm3_coef[, 2], lm4_coef[, 2], lm5_coef[, 2]),
  type = "latex",
  out = "Final/Appendix/demcand_alladsbypid_ols.tex",
  column.labels = c(
    "Dems",
    "Ind-Dem",
    "Ind",
    "Ind-Rep",
    "Reps"
  ),
  dep.var.caption = "DV: Sentiment towards Ad (1-5)",
  dep.var.labels = "Subgroups: Individual Party ID/Lean",
  covariate.labels = c(
    "Moderate Econ.",
    "Progressive Econ.",
    "Pro-Legal",
    "Pro-Undocumented"
  )
)




### Republican candidate


## Using all outcome variables

ad1.df <- study2.cleaned.df %>%
  dplyr::select(party_id_str,
    party_id,
    pid_7pt = pid_num,
    lf_num = lat_linked_fate_numeric,
    natlorigin_clean,
    imm_gen,
    undoc_know,
    dv_ad = dv_controlrep,
    ResponseId
  )

ad1.df$Treatment <- c(rep("Control"))

ad2.df <- study2.cleaned.df %>%
  dplyr::select(party_id_str,
    party_id,
    pid_7pt = pid_num,
    lf_num = lat_linked_fate_numeric,
    natlorigin_clean,
    imm_gen,
    undoc_know,
    dv_ad = dv_ad7,
    Treatment = t_7,
    ResponseId
  )

ad3.df <- study2.cleaned.df %>%
  dplyr::select(party_id_str,
    party_id,
    pid_7pt = pid_num,
    lf_num = lat_linked_fate_numeric,
    natlorigin_clean,
    imm_gen,
    undoc_know,
    dv_ad = dv_ad8,
    Treatment = t_8,
    ResponseId
  )

ad4.df <- study2.cleaned.df %>%
  dplyr::select(party_id_str,
    party_id,
    pid_7pt = pid_num,
    lf_num = lat_linked_fate_numeric,
    natlorigin_clean,
    imm_gen,
    undoc_know,
    dv_ad = dv_ad9,
    Treatment = t_9,
    ResponseId
  )

ad5.df <- study2.cleaned.df %>%
  dplyr::select(party_id_str,
    party_id,
    pid_7pt = pid_num,
    lf_num = lat_linked_fate_numeric,
    natlorigin_clean,
    imm_gen,
    undoc_know,
    dv_ad = dv_ad10,
    Treatment = t_10,
    ResponseId
  )


repads_df <- rbind(
  ad1.df,
  ad2.df,
  ad3.df,
  ad4.df,
  ad5.df
)


### Calculate OLS by PID
### Standard errors are re-calculated, clustered at respondent level

df1 <- repads_df %>% filter(party_id == "Democrat")

lm1 <- lm(dv_ad ~ Treatment, data = df1)


lm1_coef <- coeftest(lm1,
  vcov = vcovCL(lm1, factor(df1$ResponseId))
)


df2 <- repads_df %>% filter(party_id == "Independent lean Dem")

lm2 <- lm(dv_ad ~ Treatment, data = df2)

lm2_coef <- coeftest(lm2,
  vcov = vcovCL(lm2, factor(df2$ResponseId))
)



df3 <- repads_df %>% filter(party_id == "Independent no lean")

lm3 <- lm(dv_ad ~ Treatment, data = df3)

lm3_coef <- coeftest(lm3,
  vcov = vcovCL(lm3, factor(df3$ResponseId))
)


df4 <- repads_df %>% filter(party_id == "Independent lean Rep")

lm4 <- lm(dv_ad ~ Treatment, data = df4)

lm4_coef <- coeftest(lm4,
  vcov = vcovCL(lm4, factor(df4$ResponseId))
)

df5 <- repads_df %>% filter(party_id == "Republican")

lm5 <- lm(dv_ad ~ Treatment, data = df5)

lm5_coef <- coeftest(lm5,
  vcov = vcovCL(lm5, factor(df5$ResponseId))
)


stargazer(lm1, lm2, lm3, lm4, lm5,
  se = list(lm1_coef[, 2], lm2_coef[, 2], lm3_coef[, 2], lm4_coef[, 2], lm5_coef[, 2]),
  type = "latex",
  out = "Final/Appendix/repcand_alladsbypid_ols.tex",
  column.labels = c(
    "Dems",
    "Ind-Dem",
    "Ind",
    "Ind-Rep",
    "Reps"
  ),
  dep.var.caption = "DV: Sentiment towards Ad (1-5)",
  dep.var.labels = "Subgroups: Individual Party ID/Lean",
  covariate.labels = c(
    "Moderate Econ.",
    "Pro-Business",
    "Pro-Legal",
    "Anti-Undocumented"
  )
)

```




```{r Study 2 Appendix Latino Linked Fate x PID (Descriptive)}
partyidbylinkedfate.df <- study2.cleaned.df %>%
  group_by(
    party_id,
    lat_linked_fate_old
  ) %>%
  summarize(count = n()) %>%
  rename(`Latino Linked Fate` = lat_linked_fate_old)

## Fix factors
## Go from low to high

partyidbylinkedfate.df$`Latino Linked Fate` <- factor(partyidbylinkedfate.df$`Latino Linked Fate`,
  levels = c(
    "None at all",
    "A little",
    "A moderate amount",
    "A lot",
    "A great deal"
  )
)

partyidbylinkedfate.df$party_id <- factor(partyidbylinkedfate.df$party_id,
  levels = c(
    "Democrat",
    "Independent lean Dem",
    "Independent no lean",
    "Independent lean Rep",
    "Republican"
  )
)


ggplot(
  data = partyidbylinkedfate.df,
  aes(
    x = `Latino Linked Fate`,
    y = count,
    fill = `Latino Linked Fate`
  )
) +
  geom_bar(stat = "identity") +
  facet_wrap(~party_id,
    ncol = 3
  ) +
  theme(axis.text.x = element_blank()) +
  labs(
    x = "Response to Latino Linked Fate Question by Respondent Party ID",
    y = "Number of Respondents"
  ) +
  theme(
    legend.position = c(
      0.85,
      0.25
    ),
    legend.title = element_text(size = 18),
    legend.text = element_text(size = 16)
  )

ggsave("Final/Appendix/study2_latlinkedfate_by_partyid.png",
  height = 6,
  width = 10
)
```


```{r Study 2 Appendix Democratic Candidate Message by PID x Linked Fate}


## For this model, I collapse linked fate due to the distribution above:
## I have sufficient highest (5) and this group is important to distinguish

## 1: Low linked fate
## 2-3: Medium linked fate
## 4: Medium-high
## 5: High linked fate


## Democratic candidate

## Significance tests
# Clustering SEs by respondent


### 1 linked fate

# Democrat
df1 <- dempooled.df %>% filter(
  party_id == "Democrat",
  lf_num %in% c(1)
)

olr1 <- polr(dv_ad2_ord ~ t_2,
  data = df1,
  Hess = TRUE
)

olr_coef1 <- coeftest(olr1,
  vcov = vcovCL(olr1, factor(df1$ResponseId))
)


# Independent
df2 <- dempooled.df %>% filter(
  party_id %in% c(
    "Independent lean Dem",
    "Independent no lean",
    "Independent lean Rep"
  ),
  lf_num %in% c(1, 2)
)

olr2 <- polr(dv_ad2_ord ~ t_2,
  data = df2,
  Hess = TRUE
)

olr_coef2 <- coeftest(olr2,
  vcov = vcovCL(olr2, factor(df2$ResponseId))
)

# Republican
df3 <- dempooled.df %>% filter(
  party_id == "Republican",
  lf_num %in% c(1)
)

olr3 <- polr(dv_ad2_ord ~ t_2,
  data = df3,
  Hess = TRUE
)

olr_coef3 <- coeftest(olr3,
  vcov = vcovCL(olr3, factor(df3$ResponseId))
)


### 2-3 linked fate

# Democrat
df4 <- dempooled.df %>% filter(
  party_id == "Democrat",
  lf_num %in% c(2,3)
)

olr4 <- polr(dv_ad2_ord ~ t_2,
  data = df4,
  Hess = TRUE
)

olr_coef4 <- coeftest(olr1,
  vcov = vcovCL(olr4, factor(df4$ResponseId))
)


# Independent
df5 <- dempooled.df %>% filter(
  party_id %in% c(
    "Independent lean Dem",
    "Independent no lean",
    "Independent lean Rep"
  ),
  lf_num %in% c(2,3)
)

olr5 <- polr(dv_ad2_ord ~ t_2,
  data = df5,
  Hess = TRUE
)

olr_coef5 <- coeftest(olr5,
  vcov = vcovCL(olr5, factor(df5$ResponseId))
)

# Republican
df6 <- dempooled.df %>% filter(
  party_id == "Republican",
  lf_num %in% c(2,3)
)

olr6 <- polr(dv_ad2_ord ~ t_2,
  data = df6,
  Hess = TRUE
)

olr_coef6 <- coeftest(olr6,
  vcov = vcovCL(olr6, factor(df6$ResponseId))
)

### 4 linked fate

# Democrat
df7 <- dempooled.df %>% filter(
  party_id == "Democrat",
  lf_num %in% c(4)
)

olr7 <- polr(dv_ad2_ord ~ t_2,
  data = df7,
  Hess = TRUE
)

olr_coef7 <- coeftest(olr7,
  vcov = vcovCL(olr7, factor(df7$ResponseId))
)


# Independent
df8 <- dempooled.df %>% filter(
  party_id %in% c(
    "Independent lean Dem",
    "Independent no lean",
    "Independent lean Rep"
  ),
  lf_num %in% c(4)
)

olr8 <- polr(dv_ad2_ord ~ t_2,
  data = df8,
  Hess = TRUE
)

olr_coef8 <- coeftest(olr8,
  vcov = vcovCL(olr8, factor(df8$ResponseId))
)

# Republican
df9 <- dempooled.df %>% filter(
  party_id == "Republican",
  lf_num %in% c(4)
)

olr9 <- polr(dv_ad2_ord ~ t_2,
  data = df9,
  Hess = TRUE
)

olr_coef9 <- coeftest(olr9,
  vcov = vcovCL(olr9, factor(df9$ResponseId))
)



### Highest linked fate

# Democrat
# df7 <- dempooled.df %>% filter(party_id=="Democrat",
#                                lf_num == 5)

## Cannot find "suitable starting point"
## The level of support is already so high (Democrats with high linked fate) so model fails to estimate
## This is consistent with online help on GLM
## See https://stackoverflow.com/questions/8596160/why-am-i-getting-algorithm-did-not-converge-and-fitted-prob-numerically-0-or

## I report these models as 0; should be interpreted as "there is no detectable message effect because support is already so high"

#
# olr7 <- polr(dv_ad2_ord ~ t_2,
#      data = df7,
#      Hess=TRUE)

# olr_coef7 <- coeftest(olr7,
#          vcov = vcovCL(olr7, factor(df7$ResponseId) ))


# Independent
df11 <- dempooled.df %>% filter(
  party_id %in% c(
    "Independent lean Dem",
    "Independent no lean",
    "Independent lean Rep"
  ),
  lf_num == 5
)

olr11 <- polr(dv_ad2_ord ~ t_2,
  data = df11,
  Hess = TRUE
)

olr_coef11 <- coeftest(olr11,
  vcov = vcovCL(olr11, factor(df11$ResponseId))
)

# Republican
df12 <- dempooled.df %>% filter(
  party_id == "Republican",
  lf_num == 5
)

olr12 <- polr(dv_ad2_ord ~ t_2,
  data = df12,
  Hess = TRUE
)

olr_coef12 <- coeftest(olr12,
  vcov = vcovCL(olr12, factor(df12$ResponseId))
)



## None at all (1)

df1 <- tibble(
  Message = c(
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = olr_coef1[, 1],
  se = olr_coef1[, 2],
  pid = c(rep("Democrat")),
  linkedfate = c(rep("None"))
)

df2 <- tibble(
  Message = c(
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = olr_coef2[, 1],
  se = olr_coef2[, 2],
  pid = c(rep("Independent")),
  linkedfate = c(rep("None"))
)



df3 <- tibble(
  Message = c(
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = olr_coef3[, 1],
  se = olr_coef3[, 2],
  pid = c(rep("Republican")),
  linkedfate = c(rep("None"))
)


## A little/Moderate (2,3)


df4 <- tibble(
  Message = c(
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = olr_coef4[, 1],
  se = olr_coef4[, 2],
  pid = c(rep("Democrat")),
  linkedfate = c("A little/Moderate")
)

df5 <- tibble(
  Message = c(
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = olr_coef5[, 1],
  se = olr_coef5[, 2],
  pid = c(rep("Independent")),
  linkedfate = c("A little/Moderate")
)


df6 <- tibble(
  Message = c(
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = olr_coef6[, 1],
  se = olr_coef6[, 2],
  pid = c(rep("Republican")),
  linkedfate = c("A little/Moderate")
)


## A lot


df7 <- tibble(
  Message = c(
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = olr_coef7[, 1],
  se = olr_coef7[, 2],
  pid = c(rep("Democrat")),
  linkedfate = c("A lot")
)

df8 <- tibble(
  Message = c(
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = olr_coef8[, 1],
  se = olr_coef8[, 2],
  pid = c(rep("Independent")),
  linkedfate = c("A lot")
)


df9 <- tibble(
  Message = c(
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = olr_coef9[, 1],
  se = olr_coef9[, 2],
  pid = c(rep("Republican")),
  linkedfate = c("A lot")
)


## A great deal

df10 <- tibble(
  Message = c(
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = rep(c(0)),
  se = rep(c(0)),
  pid = c(rep("Democrat")),
  linkedfate = c("A great deal")
)

df11 <- tibble(
  Message = c(
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = olr_coef11[, 1],
  se = olr_coef11[, 2],
  pid = c(rep("Independent")),
  linkedfate = c("A great deal")
)


df12 <- tibble(
  Message = c(
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = olr_coef12[, 1],
  se = olr_coef12[, 2],
  pid = c(rep("Republican")),
  linkedfate = c("A great deal")
)


demcand.df <- rbind(df1, df2, df3, df4, df5, df6, df7, df8, df9, df10, df11, df12)

## Final dataclean

# Fixing factor levels

demcand.df$pid <- factor(demcand.df$pid,
  levels = c(
    "Democrat",
    "Independent",
    "Republican"
  )
)

demcand.df$Message <- factor(demcand.df$Message,
  levels = c(
    "Pro-Undocumented (D)",
    "Pro-Legal (D)",
    "Progressive Econ. (D)",
    "Moderate Econ. (D)"
  )
)

## Issue with "None" and "Pro-Legal"

demcand.df <- demcand.df %>%
  filter(linkedfate != "None" |
         Message != "Pro-Legal (D)" |
         pid != "Democrat")

## Fixing factor levels

demcand.df$linkedfate <- factor(demcand.df$linkedfate,
                                levels = c("None",
                                           "A little/Moderate",
                                           "A lot",
                                           "A great deal"))

## Graph

demcand.plot <- ggplot(
  data = demcand.df,
  aes(
    x = Message,
    y = coefficient
  )
) +
  geom_point(
    aes(
      x = Message,
      y = coefficient,
      color = Message
    ),
    size = 4
  ) +
  geom_linerange(
    aes(
      x = Message,
      color = Message,
      ymin = coefficient - 1.96 * se,
      ymax = coefficient + 1.96 * se
    ),
    lwd = 2
  ) +
  facet_grid(linkedfate ~ pid) +
  coord_flip() +
  labs(
    x = "",
    y = "Estimated Effect of Treatment Assignment on Change in Treatment Ad Outcome (1-5)"
  ) +
  theme(legend.position = "none")

## Adding constant line at 0

demcand.plot + geom_hline(
  data = demcand.df,
  aes(yintercept = 0)
)

ggsave("Final/Appendix/study2_demcandfigure_bylatlinkedfate.png",
  height = 6,
  width = 12
)
```



```{r Study 2 Appendix Republican Candidate Message by PID x Linked Fate}


## Separate models for each party ID subgroup x Spanish language ability

## 1 = No Spanish
## 2 = Some

## For this model, I collapse linked fate due to the distribution above:
## I have sufficient highest (5) and this group is important to distinguish

## 1: Low linked fate
## 2-3: Medium linked fate
## 4: Medium-high
## 5: High linked fate


## Democratic candidate

## Significance tests
# Clustering SEs by respondent

### 1 linked fate

# Democrat
df1 <- reppooled.df %>% filter(
  party_id == "Democrat",
  lf_num %in% c(1)
)

olr1 <- polr(dv_ad7_ord ~ t_7,
  data = df1,
  Hess = TRUE
)

olr_coef1 <- coeftest(olr1,
  vcov = vcovCL(olr1, factor(df1$ResponseId))
)


# Independent
df2 <- reppooled.df %>% filter(
  party_id %in% c(
    "Independent lean Dem",
    "Independent no lean",
    "Independent lean Rep"
  ),
  lf_num %in% c(1, 2)
)

olr2 <- polr(dv_ad7_ord ~ t_7,
  data = df2,
  Hess = TRUE
)

olr_coef2 <- coeftest(olr2,
  vcov = vcovCL(olr2, factor(df2$ResponseId))
)

# Republican
df3 <- reppooled.df %>% filter(
  party_id == "Republican",
  lf_num %in% c(1)
)

olr3 <- polr(dv_ad7_ord ~ t_7,
  data = df3,
  Hess = TRUE
)

olr_coef3 <- coeftest(olr3,
  vcov = vcovCL(olr3, factor(df3$ResponseId))
)


### 2-3 linked fate

# Democrat
df4 <- reppooled.df %>% filter(
  party_id == "Democrat",
  lf_num %in% c(2,3)
)

olr4 <- polr(dv_ad7_ord ~ t_7,
  data = df4,
  Hess = TRUE
)

olr_coef4 <- coeftest(olr1,
  vcov = vcovCL(olr4, factor(df4$ResponseId))
)


# Independent
df5 <- reppooled.df %>% filter(
  party_id %in% c(
    "Independent lean Dem",
    "Independent no lean",
    "Independent lean Rep"
  ),
  lf_num %in% c(2,3)
)

olr5 <- polr(dv_ad7_ord ~ t_7,
  data = df5,
  Hess = TRUE
)

olr_coef5 <- coeftest(olr5,
  vcov = vcovCL(olr5, factor(df5$ResponseId))
)

# Republican
df6 <- reppooled.df %>% filter(
  party_id == "Republican",
  lf_num %in% c(2,3)
)

olr6 <- polr(dv_ad7_ord ~ t_7,
  data = df6,
  Hess = TRUE
)

olr_coef6 <- coeftest(olr6,
  vcov = vcovCL(olr6, factor(df6$ResponseId))
)

### 4 linked fate

# Democrat
df7 <- reppooled.df %>% filter(
  party_id == "Democrat",
  lf_num %in% c(4)
)

olr7 <- polr(dv_ad7_ord ~ t_7,
  data = df7,
  Hess = TRUE
)

olr_coef7 <- coeftest(olr7,
  vcov = vcovCL(olr7, factor(df7$ResponseId))
)


# Independent
df8 <- reppooled.df %>% filter(
  party_id %in% c(
    "Independent lean Dem",
    "Independent no lean",
    "Independent lean Rep"
  ),
  lf_num %in% c(4)
)

olr8 <- polr(dv_ad7_ord ~ t_7,
  data = df8,
  Hess = TRUE
)

olr_coef8 <- coeftest(olr8,
  vcov = vcovCL(olr8, factor(df8$ResponseId))
)

# Republican
df9 <- reppooled.df %>% filter(
  party_id == "Republican",
  lf_num %in% c(4)
)

olr9 <- polr(dv_ad7_ord ~ t_7,
  data = df9,
  Hess = TRUE
)

olr_coef9 <- coeftest(olr9,
  vcov = vcovCL(olr9, factor(df9$ResponseId))
)



### Highest linked fate

# Democrat
# df7 <- reppooled.df %>% filter(party_id=="Democrat",
#                                lf_num == 5)

## Cannot find "suitable starting point"
## The level of support is already so high (Democrats with high linked fate) so model fails to estimate
## This is consistent with online help on GLM
## See https://stackoverflow.com/questions/8596160/why-am-i-getting-algorithm-did-not-converge-and-fitted-prob-numerically-0-or

## I report these models as 0; should be interpreted as "there is no detectable message effect because support is already so high"

#
# olr7 <- polr(dv_ad7_ord ~ t_7,
#      data = df7,
#      Hess=TRUE)

# olr_coef7 <- coeftest(olr7,
#          vcov = vcovCL(olr7, factor(df7$ResponseId) ))


# Independent
df11 <- reppooled.df %>% filter(
  party_id %in% c(
    "Independent lean Dem",
    "Independent no lean",
    "Independent lean Rep"
  ),
  lf_num == 5
)

olr11 <- polr(dv_ad7_ord ~ t_7,
  data = df11,
  Hess = TRUE
)

olr_coef11 <- coeftest(olr11,
  vcov = vcovCL(olr11, factor(df11$ResponseId))
)

# Republican
df12 <- reppooled.df %>% filter(
  party_id == "Republican",
  lf_num == 5
)

olr12 <- polr(dv_ad7_ord ~ t_7,
  data = df12,
  Hess = TRUE
)

olr_coef12 <- coeftest(olr12,
  vcov = vcovCL(olr12, factor(df12$ResponseId))
)



## None at all (1)

df1 <- tibble(
  Message = c(
    "Moderate Econ. (R)",
    "Pro-Business Econ. (R)",
    "Pro-Legal (R)",
    "Anti-Undocumented (R)"
  ),
  coefficient = olr_coef1[, 1],
  se = olr_coef1[, 2],
  pid = c(rep("Democrat")),
  linkedfate = c(rep("None"))
)

df2 <- tibble(
  Message = c(
    "Moderate Econ. (R)",
    "Pro-Business Econ. (R)",
    "Pro-Legal (R)",
    "Anti-Undocumented (R)"
  ),
  coefficient = olr_coef2[, 1],
  se = olr_coef2[, 2],
  pid = c(rep("Independent")),
  linkedfate = c(rep("None"))
)



df3 <- tibble(
  Message = c(
    "Moderate Econ. (R)",
    "Pro-Business Econ. (R)",
    "Pro-Legal (R)",
    "Anti-Undocumented (R)"
  ),
  coefficient = olr_coef3[, 1],
  se = olr_coef3[, 2],
  pid = c(rep("Republican")),
  linkedfate = c(rep("None"))
)


## A little/Moderate (2,3)


df4 <- tibble(
  Message = c(
    "Moderate Econ. (R)",
    "Pro-Business Econ. (R)",
    "Pro-Legal (R)",
    "Anti-Undocumented (R)"
  ),
  coefficient = olr_coef4[, 1],
  se = olr_coef4[, 2],
  pid = c(rep("Democrat")),
  linkedfate = c("A little/Moderate")
)

df5 <- tibble(
  Message = c(
    "Moderate Econ. (R)",
    "Pro-Business Econ. (R)",
    "Pro-Legal (R)",
    "Anti-Undocumented (R)"
  ),
  coefficient = olr_coef5[, 1],
  se = olr_coef5[, 2],
  pid = c(rep("Independent")),
  linkedfate = c("A little/Moderate")
)


df6 <- tibble(
  Message = c(
    "Moderate Econ. (R)",
    "Pro-Business Econ. (R)",
    "Pro-Legal (R)",
    "Anti-Undocumented (R)"
  ),
  coefficient = olr_coef6[, 1],
  se = olr_coef6[, 2],
  pid = c(rep("Republican")),
  linkedfate = c("A little/Moderate")
)


## A lot


df7 <- tibble(
  Message = c(
    "Moderate Econ. (R)",
    "Pro-Business Econ. (R)",
    "Pro-Legal (R)",
    "Anti-Undocumented (R)"
  ),
  coefficient = olr_coef7[, 1],
  se = olr_coef7[, 2],
  pid = c(rep("Democrat")),
  linkedfate = c("A lot")
)

df8 <- tibble(
  Message = c(
    "Moderate Econ. (R)",
    "Pro-Business Econ. (R)",
    "Pro-Legal (R)",
    "Anti-Undocumented (R)"
  ),
  coefficient = olr_coef8[, 1],
  se = olr_coef8[, 2],
  pid = c(rep("Independent")),
  linkedfate = c("A lot")
)


df9 <- tibble(
  Message = c(
    "Moderate Econ. (R)",
    "Pro-Business Econ. (R)",
    "Pro-Legal (R)",
    "Anti-Undocumented (R)"
  ),
  coefficient = olr_coef9[, 1],
  se = olr_coef9[, 2],
  pid = c(rep("Republican")),
  linkedfate = c("A lot")
)


## A great deal

df10 <- tibble(
  Message = c(
    "Moderate Econ. (R)",
    "Pro-Business Econ. (R)",
    "Pro-Legal (R)",
    "Anti-Undocumented (R)"
  ),
  coefficient = rep(c(0)),
  se = rep(c(0)),
  pid = c(rep("Democrat")),
  linkedfate = c("A great deal")
)

df11 <- tibble(
  Message = c(
    "Moderate Econ. (R)",
    "Pro-Business Econ. (R)",
    "Pro-Legal (R)",
    "Anti-Undocumented (R)"
  ),
  coefficient = olr_coef11[, 1],
  se = olr_coef11[, 2],
  pid = c(rep("Independent")),
  linkedfate = c("A great deal")
)


df12 <- tibble(
  Message = c(
    "Moderate Econ. (R)",
    "Pro-Business Econ. (R)",
    "Pro-Legal (R)",
    "Anti-Undocumented (R)"
  ),
  coefficient = olr_coef12[, 1],
  se = olr_coef12[, 2],
  pid = c(rep("Republican")),
  linkedfate = c("A great deal")
)


repcand.df <- rbind(df1, df2, df3, df4, df5, df6, df7, df8, df9, df10, df11, df12)

## Final dataclean

# Fixing factor levels

repcand.df$pid <- factor(repcand.df$pid,
  levels = c(
    "Democrat",
    "Independent",
    "Republican"
  )
)

repcand.df$Message <- factor(repcand.df$Message,
  levels = c(
    "Anti-Undocumented (R)",
    "Pro-Legal (R)",
    "Pro-Business Econ. (R)",
    "Moderate Econ. (R)"
  )
)

## Issue with "None" and "Pro-Legal"

repcand.df <- repcand.df %>%
  filter(linkedfate != "None" |
         Message != "Pro-Legal (D)" |
         pid != "Democrat")

## Fixing factor levels

repcand.df$linkedfate <- factor(repcand.df$linkedfate,
                                levels = c("None",
                                           "A little/Moderate",
                                           "A lot",
                                           "A great deal"))

## Graph

repcand.plot <- ggplot(
  data = repcand.df,
  aes(
    x = Message,
    y = coefficient
  )
) +
  geom_point(
    aes(
      x = Message,
      y = coefficient,
      color = Message
    ),
    size = 4
  ) +
  geom_linerange(
    aes(
      x = Message,
      color = Message,
      ymin = coefficient - 1.96 * se,
      ymax = coefficient + 1.96 * se
    ),
    lwd = 2
  ) +
  facet_grid(linkedfate ~ pid) +
  coord_flip() +
  labs(
    x = "",
    y = "Estimated Effect of Treatment Assignment on Change in Treatment Ad Outcome (1-5)"
  ) +
  theme(legend.position = "none")

## Adding constant line at 0

repcand.plot + geom_hline(
  data = repcand.df,
  aes(yintercept = 0)
)

ggsave("Final/Appendix/study2_repcandfigure_bylatlinkedfate.png",
  height = 6,
  width = 12
)
```



```{r Study 2 Appendix Democratic Candidate Message by PID x Language}


## Separate models for each party ID subgroup x Spanish language ability

diag_df <- dempooled.df %>%
  group_by(english_skills) %>%
  reframe(count=n()/2)

diag_df <- dempooled.df %>%
  group_by(spanish_skills) %>%
  reframe(count=n()/2)

## 1 = Not at all
## 2 = Just a little
## 3 = Somewhat well
## 4 = Very well
## 5 = Perfectly

## Democratic candidate

## Significance tests
# Clustering SEs by respondent

### Spanish skills = 1

# Democrat
df1 <- dempooled.df %>% filter(
  party_id == "Democrat" &
  spanish_skills == "Not at all"
)

olr1 <- polr(dv_ad2_ord ~ t_2,
  data = df1,
  Hess = TRUE
)

olr_coef1 <- coeftest(olr1,
  vcov = vcovCL(olr1, factor(df1$ResponseId))
)


# Independent
df2 <- dempooled.df %>% filter(
  party_id %in% c(
    "Independent lean Dem",
    "Independent no lean",
    "Independent lean Rep"
  ),
  spanish_skills == "Not at all"
)

olr2 <- polr(dv_ad2_ord ~ t_2,
  data = df2,
  Hess = TRUE
)

olr_coef2 <- coeftest(olr2,
  vcov = vcovCL(olr2, factor(df2$ResponseId))
)

# Republican
df3 <- dempooled.df %>% filter(
  party_id == "Republican",
  spanish_skills == "Not at all"
)

olr3 <- polr(dv_ad2_ord ~ t_2,
  data = df3,
  Hess = TRUE
)

olr_coef3 <- coeftest(olr3,
  vcov = vcovCL(olr3, factor(df3$ResponseId))
)


### Spanish = 2

# Democrat
df4 <- dempooled.df %>% filter(
  party_id == "Democrat",
  spanish_skills == "Just a little"
)

olr4 <- polr(dv_ad2_ord ~ t_2,
  data = df4,
  Hess = TRUE
)

olr_coef4 <- coeftest(olr1,
  vcov = vcovCL(olr4, factor(df4$ResponseId))
)


# Independent
df5 <- dempooled.df %>% filter(
  party_id %in% c(
    "Independent lean Dem",
    "Independent no lean",
    "Independent lean Rep"
  ),
  spanish_skills == "Just a little"
)

olr5 <- polr(dv_ad2_ord ~ t_2,
  data = df5,
  Hess = TRUE
)

olr_coef5 <- coeftest(olr5,
  vcov = vcovCL(olr5, factor(df5$ResponseId))
)

# Republican
df6 <- dempooled.df %>% filter(
  party_id == "Republican",
  spanish_skills == "Just a little"
)

olr6 <- polr(dv_ad2_ord ~ t_2,
  data = df6,
  Hess = TRUE
)

olr_coef6 <- coeftest(olr6,
  vcov = vcovCL(olr6, factor(df6$ResponseId))
)

### Spanish = 3

# Democrat
df7 <- dempooled.df %>% filter(
  party_id == "Democrat",
  spanish_skills == "Somewhat well"
)

olr7 <- polr(dv_ad2_ord ~ t_2,
  data = df7,
  Hess = TRUE
)

olr_coef7 <- coeftest(olr7,
  vcov = vcovCL(olr7, factor(df7$ResponseId))
)


# Independent
df8 <- dempooled.df %>% filter(
  party_id %in% c(
    "Independent lean Dem",
    "Independent no lean",
    "Independent lean Rep"
  ),
  spanish_skills == "Somewhat well"
)

olr8 <- polr(dv_ad2_ord ~ t_2,
  data = df8,
  Hess = TRUE
)

olr_coef8 <- coeftest(olr8,
  vcov = vcovCL(olr8, factor(df8$ResponseId))
)

# Republican
df9 <- dempooled.df %>% filter(
  party_id == "Republican",
  spanish_skills == "Somewhat well"
)

olr9 <- polr(dv_ad2_ord ~ t_2,
  data = df9,
  Hess = TRUE
)

olr_coef9 <- coeftest(olr9,
  vcov = vcovCL(olr9, factor(df9$ResponseId))
)



### Spanish = 4

## Democrat
 df10 <- dempooled.df %>% filter(party_id=="Democrat",
  spanish_skills == "Very well")



 olr10 <- polr(dv_ad2_ord ~ t_2,
      data = df10,
      Hess=TRUE)

 olr_coef10 <- coeftest(olr10,
          vcov = vcovCL(olr10, factor(df10$ResponseId) ))


# Independent
df11 <- dempooled.df %>% filter(
  party_id %in% c(
    "Independent lean Dem",
    "Independent no lean",
    "Independent lean Rep"
  ),
  spanish_skills == "Very well"
)

olr11 <- polr(dv_ad2_ord ~ t_2,
  data = df11,
  Hess = TRUE
)

olr_coef11 <- coeftest(olr11,
  vcov = vcovCL(olr11, factor(df11$ResponseId))
)

# Republican
df12 <- dempooled.df %>% filter(
  party_id == "Republican",
  spanish_skills == "Very well"
)

olr12 <- polr(dv_ad2_ord ~ t_2,
  data = df12,
  Hess = TRUE
)

olr_coef12 <- coeftest(olr12,
  vcov = vcovCL(olr12, factor(df12$ResponseId))
)



### Spanish = 5

## Democrat
 df13 <- dempooled.df %>% filter(party_id=="Democrat",
  spanish_skills == "Perfectly")



 olr13 <- polr(dv_ad2_ord ~ t_2,
      data = df13,
      Hess=TRUE)

 olr_coef13 <- coeftest(olr13,
          vcov = vcovCL(olr13, factor(df13$ResponseId) ))


# Independent
df14 <- dempooled.df %>% filter(
  party_id %in% c(
    "Independent lean Dem",
    "Independent no lean",
    "Independent lean Rep"
  ),
  spanish_skills == "Perfectly"
)

olr14 <- polr(dv_ad2_ord ~ t_2,
  data = df14,
  Hess = TRUE
)

olr_coef14 <- coeftest(olr14,
  vcov = vcovCL(olr14, factor(df14$ResponseId))
)

# Republican
df15 <- dempooled.df %>% filter(
  party_id == "Republican",
  spanish_skills == "Perfectly"
)

olr15 <- polr(dv_ad2_ord ~ t_2,
  data = df15,
  Hess = TRUE
)

olr_coef15 <- coeftest(olr15,
  vcov = vcovCL(olr15, factor(df15$ResponseId))
)



## None at all (1)

df1 <- tibble(
  Message = c(
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = olr_coef1[, 1],
  se = olr_coef1[, 2],
  pid = c(rep("Democrat")),
  spanish = c(rep("None"))
  )

df2 <- tibble(
  Message = c(
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = olr_coef2[, 1],
  se = olr_coef2[, 2],
  pid = c(rep("Independent")),
  spanish = c(rep("None"))
)



df3 <- tibble(
  Message = c(
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = olr_coef3[, 1],
  se = olr_coef3[, 2],
  pid = c(rep("Republican")),
  spanish = c(rep("None"))
)


## Just a little


df4 <- tibble(
  Message = c(
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = olr_coef4[, 1],
  se = olr_coef4[, 2],
  pid = c(rep("Democrat")),
  spanish = c(rep("A little"))
)

df5 <- tibble(
  Message = c(
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = olr_coef5[, 1],
  se = olr_coef5[, 2],
  pid = c(rep("Independent")),
  spanish = c(rep("A little"))
)


df6 <- tibble(
  Message = c(
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = olr_coef6[, 1],
  se = olr_coef6[, 2],
  pid = c(rep("Republican")),
  spanish = c(rep("A little"))
)


## Somewhat


df7 <- tibble(
  Message = c(
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = olr_coef7[, 1],
  se = olr_coef7[, 2],
  pid = c(rep("Democrat")),
  spanish = c("Somewhat")
)

df8 <- tibble(
  Message = c(
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = olr_coef8[, 1],
  se = olr_coef8[, 2],
  pid = c(rep("Independent")),
  spanish = c("Somewhat")
)


df9 <- tibble(
  Message = c(
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = olr_coef9[, 1],
  se = olr_coef9[, 2],
  pid = c(rep("Republican")),
  spanish = c("Somewhat")
)


## Very well

df10 <- tibble(
  Message = c(
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = olr_coef10[, 1],
  se = olr_coef10[, 2],
  pid = c(rep("Democrat")),
  spanish = c("Very well")
)

df11 <- tibble(
  Message = c(
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = olr_coef11[, 1],
  se = olr_coef11[, 2],
  pid = c(rep("Independent")),
  spanish = c("Very well")
)


df12 <- tibble(
  Message = c(
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = olr_coef12[, 1],
  se = olr_coef12[, 2],
  pid = c(rep("Republican")),
  spanish = c("Very well")
)


## Perfectly

df13 <- tibble(
  Message = c(
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = olr_coef13[, 1],
  se = olr_coef13[, 2],
  pid = c(rep("Democrat")),
  spanish = c("Perfectly")
)

df14 <- tibble(
  Message = c(
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = olr_coef14[, 1],
  se = olr_coef14[, 2],
  pid = c(rep("Independent")),
  spanish = c("Perfectly")
)


df15 <- tibble(
  Message = c(
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = olr_coef15[, 1],
  se = olr_coef15[, 2],
  pid = c(rep("Republican")),
  spanish = c("Perfectly")
)

demcand.df <- rbind(df1, df2, df3, 
                    df4, df5, df6, 
                    df7, df8, df9, 
                    df10, df11, df12,
                    df13, df14, df15)


## Final dataclean

# Fixing factor levels

demcand.df$pid <- factor(demcand.df$pid,
  levels = c(
    "Democrat",
    "Independent",
    "Republican"
  )
)

demcand.df$Message <- factor(demcand.df$Message,
  levels = c(
    "Pro-Undocumented (D)",
    "Pro-Legal (D)",
    "Progressive Econ. (D)",
    "Moderate Econ. (D)"
  )
)

## Fixing factor levels

demcand.df$Spanish <- factor(demcand.df$spanish,
                             levels = c("None",
                                        "A little",
                                        "Somewhat",
                                        "Very well",
                                        "Perfectly"))

## Graph

demcand.plot <- ggplot(
  data = demcand.df,
  aes(
    x = Message,
    y = coefficient
  )
) +
  geom_point(
    aes(
      x = Message,
      y = coefficient,
      color = Message
    ),
    size = 4
  ) +
  geom_linerange(
    aes(
      x = Message,
      color = Message,
      ymin = coefficient - 1.96 * se,
      ymax = coefficient + 1.96 * se
    ),
    lwd = 2
  ) +
  facet_grid(Spanish ~ pid) +
  coord_flip() +
  labs(
    x = "",
    y = "Estimated Effect of Treatment Assignment on Change in Treatment Ad Outcome (1-5)"
  ) +
  theme(legend.position = "none")

## Adding constant line at 0

demcand.plot + geom_hline(
  data = demcand.df,
  aes(yintercept = 0)
)

ggsave("Final/Appendix/study2_demcandfigure_bylanguage.png",
  height = 6,
  width = 12
)
```



```{r Study 2 Appendix Republican Candidate Message by PID x Language}


## Separate models for each party ID subgroup x Spanish language ability

diag_df <- reppooled.df %>%
  group_by(english_skills) %>%
  reframe(count=n()/2)

diag_df <- reppooled.df %>%
  group_by(spanish_skills) %>%
  reframe(count=n()/2)

## 1 = Not at all
## 2 = Just a little
## 3 = Somewhat well
## 4 = Very well
## 5 = Perfectly

## Democratic candidate

## Significance tests
# Clustering SEs by respondent

### Spanish skills = 1

# Democrat
df1 <- reppooled.df %>% filter(
  party_id == "Democrat" &
  spanish_skills == "Not at all"
)

olr1 <- polr(dv_ad7_ord ~ t_7,
  data = df1,
  Hess = TRUE
)

olr_coef1 <- coeftest(olr1,
  vcov = vcovCL(olr1, factor(df1$ResponseId))
)


# Independent
df2 <- reppooled.df %>% filter(
  party_id %in% c(
    "Independent lean Dem",
    "Independent no lean",
    "Independent lean Rep"
  ),
  spanish_skills == "Not at all"
)

olr2 <- polr(dv_ad7_ord ~ t_7,
  data = df2,
  Hess = TRUE
)

olr_coef2 <- coeftest(olr2,
  vcov = vcovCL(olr2, factor(df2$ResponseId))
)

# Republican
df3 <- reppooled.df %>% filter(
  party_id == "Republican",
  spanish_skills == "Not at all"
)

olr3 <- polr(dv_ad7_ord ~ t_7,
  data = df3,
  Hess = TRUE
)

olr_coef3 <- coeftest(olr3,
  vcov = vcovCL(olr3, factor(df3$ResponseId))
)


### Spanish = 2

# Democrat
df4 <- reppooled.df %>% filter(
  party_id == "Democrat",
  spanish_skills == "Just a little"
)

olr4 <- polr(dv_ad7_ord ~ t_7,
  data = df4,
  Hess = TRUE
)

olr_coef4 <- coeftest(olr1,
  vcov = vcovCL(olr4, factor(df4$ResponseId))
)


# Independent
df5 <- reppooled.df %>% filter(
  party_id %in% c(
    "Independent lean Dem",
    "Independent no lean",
    "Independent lean Rep"
  ),
  spanish_skills == "Just a little"
)

olr5 <- polr(dv_ad7_ord ~ t_7,
  data = df5,
  Hess = TRUE
)

olr_coef5 <- coeftest(olr5,
  vcov = vcovCL(olr5, factor(df5$ResponseId))
)

# Republican
df6 <- reppooled.df %>% filter(
  party_id == "Republican",
  spanish_skills == "Just a little"
)

olr6 <- polr(dv_ad7_ord ~ t_7,
  data = df6,
  Hess = TRUE
)

olr_coef6 <- coeftest(olr6,
  vcov = vcovCL(olr6, factor(df6$ResponseId))
)

### Spanish = 3

# Democrat
df7 <- reppooled.df %>% filter(
  party_id == "Democrat",
  spanish_skills == "Somewhat well"
)

olr7 <- polr(dv_ad7_ord ~ t_7,
  data = df7,
  Hess = TRUE
)

olr_coef7 <- coeftest(olr7,
  vcov = vcovCL(olr7, factor(df7$ResponseId))
)


# Independent
df8 <- reppooled.df %>% filter(
  party_id %in% c(
    "Independent lean Dem",
    "Independent no lean",
    "Independent lean Rep"
  ),
  spanish_skills == "Somewhat well"
)

olr8 <- polr(dv_ad7_ord ~ t_7,
  data = df8,
  Hess = TRUE
)

olr_coef8 <- coeftest(olr8,
  vcov = vcovCL(olr8, factor(df8$ResponseId))
)

# Republican
df9 <- reppooled.df %>% filter(
  party_id == "Republican",
  spanish_skills == "Somewhat well"
)

olr9 <- polr(dv_ad7_ord ~ t_7,
  data = df9,
  Hess = TRUE
)

olr_coef9 <- coeftest(olr9,
  vcov = vcovCL(olr9, factor(df9$ResponseId))
)



### Spanish = 4

## Democrat
 df10 <- reppooled.df %>% filter(party_id=="Democrat",
  spanish_skills == "Very well")



 olr10 <- polr(dv_ad7_ord ~ t_7,
      data = df10,
      Hess=TRUE)

 olr_coef10 <- coeftest(olr10,
          vcov = vcovCL(olr10, factor(df10$ResponseId) ))


# Independent
df11 <- reppooled.df %>% filter(
  party_id %in% c(
    "Independent lean Dem",
    "Independent no lean",
    "Independent lean Rep"
  ),
  spanish_skills == "Very well"
)

olr11 <- polr(dv_ad7_ord ~ t_7,
  data = df11,
  Hess = TRUE
)

olr_coef11 <- coeftest(olr11,
  vcov = vcovCL(olr11, factor(df11$ResponseId))
)

# Republican
df12 <- reppooled.df %>% filter(
  party_id == "Republican",
  spanish_skills == "Very well"
)

olr12 <- polr(dv_ad7_ord ~ t_7,
  data = df12,
  Hess = TRUE
)

olr_coef12 <- coeftest(olr12,
  vcov = vcovCL(olr12, factor(df12$ResponseId))
)



### Spanish = 5

## Democrat
 df13 <- reppooled.df %>% filter(party_id=="Democrat",
  spanish_skills == "Perfectly")



 olr13 <- polr(dv_ad7_ord ~ t_7,
      data = df13,
      Hess=TRUE)

 olr_coef13 <- coeftest(olr13,
          vcov = vcovCL(olr13, factor(df13$ResponseId) ))


# Independent
df14 <- reppooled.df %>% filter(
  party_id %in% c(
    "Independent lean Dem",
    "Independent no lean",
    "Independent lean Rep"
  ),
  spanish_skills == "Perfectly"
)

olr14 <- polr(dv_ad7_ord ~ t_7,
  data = df14,
  Hess = TRUE
)

olr_coef14 <- coeftest(olr14,
  vcov = vcovCL(olr14, factor(df14$ResponseId))
)

# Republican
df15 <- reppooled.df %>% filter(
  party_id == "Republican",
  spanish_skills == "Perfectly"
)

olr15 <- polr(dv_ad7_ord ~ t_7,
  data = df15,
  Hess = TRUE
)

olr_coef15 <- coeftest(olr15,
  vcov = vcovCL(olr15, factor(df15$ResponseId))
)



## None at all (1)

df1 <- tibble(
  Message = c(
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = olr_coef1[, 1],
  se = olr_coef1[, 2],
  pid = c(rep("Democrat")),
  spanish = c(rep("None"))
  )

df2 <- tibble(
  Message = c(
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = olr_coef2[, 1],
  se = olr_coef2[, 2],
  pid = c(rep("Independent")),
  spanish = c(rep("None"))
)



df3 <- tibble(
  Message = c(
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = olr_coef3[, 1],
  se = olr_coef3[, 2],
  pid = c(rep("Republican")),
  spanish = c(rep("None"))
)


## Just a little


df4 <- tibble(
  Message = c(
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = olr_coef4[, 1],
  se = olr_coef4[, 2],
  pid = c(rep("Democrat")),
  spanish = c(rep("A little"))
)

df5 <- tibble(
  Message = c(
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = olr_coef5[, 1],
  se = olr_coef5[, 2],
  pid = c(rep("Independent")),
  spanish = c(rep("A little"))
)


df6 <- tibble(
  Message = c(
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = olr_coef6[, 1],
  se = olr_coef6[, 2],
  pid = c(rep("Republican")),
  spanish = c(rep("A little"))
)


## Somewhat


df7 <- tibble(
  Message = c(
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = olr_coef7[, 1],
  se = olr_coef7[, 2],
  pid = c(rep("Democrat")),
  spanish = c("Somewhat")
)

df8 <- tibble(
  Message = c(
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = olr_coef8[, 1],
  se = olr_coef8[, 2],
  pid = c(rep("Independent")),
  spanish = c("Somewhat")
)


df9 <- tibble(
  Message = c(
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = olr_coef9[, 1],
  se = olr_coef9[, 2],
  pid = c(rep("Republican")),
  spanish = c("Somewhat")
)


## Very well

df10 <- tibble(
  Message = c(
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = olr_coef10[, 1],
  se = olr_coef10[, 2],
  pid = c(rep("Democrat")),
  spanish = c("Very well")
)

df11 <- tibble(
  Message = c(
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = olr_coef11[, 1],
  se = olr_coef11[, 2],
  pid = c(rep("Independent")),
  spanish = c("Very well")
)


df12 <- tibble(
  Message = c(
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = olr_coef12[, 1],
  se = olr_coef12[, 2],
  pid = c(rep("Republican")),
  spanish = c("Very well")
)


## Perfectly

df13 <- tibble(
  Message = c(
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = olr_coef13[, 1],
  se = olr_coef13[, 2],
  pid = c(rep("Democrat")),
  spanish = c("Perfectly")
)

df14 <- tibble(
  Message = c(
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = olr_coef14[, 1],
  se = olr_coef14[, 2],
  pid = c(rep("Independent")),
  spanish = c("Perfectly")
)


df15 <- tibble(
  Message = c(
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = olr_coef15[, 1],
  se = olr_coef15[, 2],
  pid = c(rep("Republican")),
  spanish = c("Perfectly")
)

repcand.df <- rbind(df1, df2, df3, 
                    df4, df5, df6, 
                    df7, df8, df9, 
                    df10, df11, df12,
                    df13, df14, df15)


## Final dataclean

# Fixing factor levels

repcand.df$pid <- factor(repcand.df$pid,
  levels = c(
    "Democrat",
    "Independent",
    "Republican"
  )
)

repcand.df$Message <- factor(repcand.df$Message,
  levels = c(
    "Pro-Undocumented (D)",
    "Pro-Legal (D)",
    "Progressive Econ. (D)",
    "Moderate Econ. (D)"
  )
)

## Fixing factor levels

repcand.df$Spanish <- factor(repcand.df$spanish,
                             levels = c("None",
                                        "A little",
                                        "Somewhat",
                                        "Very well",
                                        "Perfectly"))

## Graph

repcand.plot <- ggplot(
  data = repcand.df,
  aes(
    x = Message,
    y = coefficient
  )
) +
  geom_point(
    aes(
      x = Message,
      y = coefficient,
      color = Message
    ),
    size = 4
  ) +
  geom_linerange(
    aes(
      x = Message,
      color = Message,
      ymin = coefficient - 1.96 * se,
      ymax = coefficient + 1.96 * se
    ),
    lwd = 2
  ) +
  facet_grid(Spanish ~ pid) +
  coord_flip() +
  labs(
    x = "",
    y = "Estimated Effect of Treatment Assignment on Change in Treatment Ad Outcome (1-5)"
  ) +
  theme(legend.position = "none")

## Adding constant line at 0

repcand.plot + geom_hline(
  data = repcand.df,
  aes(yintercept = 0)
)

ggsave("Final/Appendix/study2_repcandfigure_bylanguage.png",
  height = 6,
  width = 12
)
```


```{r Study 2 Appendix Democratic Candidate Message by PID x Natl Origin}
## Separate models for each party ID subgroup x Natl Origin


## Democratic candidate

## Significance tests
# Clustering SEs by respondent


### Mexican Natl Origin

# Democrat
df1 <- dempooled.df %>% filter(
  party_id == "Democrat",
  natlorigin_clean == "Mexican"
)

olr1 <- polr(dv_ad2_ord ~ t_2,
  data = df1,
  Hess = TRUE
)

olr_coef1 <- coeftest(olr1,
  vcov = vcovCL(olr1, factor(df1$ResponseId))
)


# Independent
df2 <- dempooled.df %>% filter(
  party_id %in% c(
    "Independent lean Dem",
    "Independent no lean",
    "Independent lean Rep"
  ),
  natlorigin_clean == "Mexican"
)

olr2 <- polr(dv_ad2_ord ~ t_2,
  data = df2,
  Hess = TRUE
)

olr_coef2 <- coeftest(olr2,
  vcov = vcovCL(olr2, factor(df2$ResponseId))
)

# Republican
df3 <- dempooled.df %>% filter(
  party_id == "Republican",
  natlorigin_clean == "Mexican"
)

olr3 <- polr(dv_ad2_ord ~ t_2,
  data = df3,
  Hess = TRUE
)

olr_coef3 <- coeftest(olr3,
  vcov = vcovCL(olr3, factor(df3$ResponseId))
)



### Cuban Natl Origin

# Democrat
df4 <- dempooled.df %>% filter(
  party_id == "Democrat",
  natlorigin_clean == "Cuban"
)

olr4 <- polr(dv_ad2_ord ~ t_2,
  data = df4,
  Hess = TRUE
)

olr_coef4 <- coeftest(olr4,
  vcov = vcovCL(olr4, factor(df4$ResponseId))
)


# Independent
df5 <- dempooled.df %>% filter(
  party_id %in% c(
    "Independent lean Dem",
    "Independent no lean",
    "Independent lean Rep"
  ),
  natlorigin_clean == "Cuban"
)

olr5 <- polr(dv_ad2_ord ~ t_2,
  data = df5,
  Hess = TRUE
)

olr_coef5 <- coeftest(olr5,
  vcov = vcovCL(olr5, factor(df5$ResponseId))
)

# Republican
df6 <- dempooled.df %>% filter(
  party_id == "Republican",
  natlorigin_clean == "Cuban"
)

olr6 <- polr(dv_ad2_ord ~ t_2,
  data = df6,
  Hess = TRUE
)

olr_coef6 <- coeftest(olr6,
  vcov = vcovCL(olr6, factor(df6$ResponseId))
)



### Puerto Rican Natl Origin

# Democrat
df7 <- dempooled.df %>% filter(
  party_id == "Democrat",
  natlorigin_clean == "Puerto Rican"
)

olr7 <- polr(dv_ad2_ord ~ t_2,
  data = df7,
  Hess = TRUE
)

olr_coef7 <- coeftest(olr7,
  vcov = vcovCL(olr7, factor(df7$ResponseId))
)


# Independent
df8 <- dempooled.df %>% filter(
  party_id %in% c(
    "Independent lean Dem",
    "Independent no lean",
    "Independent lean Rep"
  ),
  natlorigin_clean == "Puerto Rican"
)

olr8 <- polr(dv_ad2_ord ~ t_2,
  data = df8,
  Hess = TRUE
)

olr_coef8 <- coeftest(olr8,
  vcov = vcovCL(olr8, factor(df8$ResponseId))
)

# Republican
df9 <- dempooled.df %>% filter(
  party_id == "Republican",
  natlorigin_clean == "Puerto Rican"
)

olr9 <- polr(dv_ad2_ord ~ t_2,
  data = df9,
  Hess = TRUE
)

olr_coef9 <- coeftest(olr9,
  vcov = vcovCL(olr9, factor(df9$ResponseId))
)





## Job Talk Study 2 Democratic Candidate Figure x Natl Origin


## Mexican

df1 <- tibble(
  Message = c(
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = olr_coef1[, 1],
  se = olr_coef1[, 2],
  pid = c(rep("Democrat")),
  natlorigin = c(rep("Mexican"))
)

df2 <- tibble(
  Message = c(
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = olr_coef2[, 1],
  se = olr_coef2[, 2],
  pid = c(rep("Independent")),
  natlorigin = c(rep("Mexican"))
)



df3 <- tibble(
  Message = c(
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = olr_coef5[, 1],
  se = olr_coef3[, 2],
  pid = c(rep("Republican")),
  natlorigin = c(rep("Mexican"))
)


## Cuban


df4 <- tibble(
  Message = c(
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = olr_coef4[, 1],
  se = olr_coef4[, 2],
  pid = c(rep("Democrat")),
  natlorigin = c(rep("Cuban"))
)

df5 <- tibble(
  Message = c(
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = olr_coef5[, 1],
  se = olr_coef5[, 2],
  pid = c(rep("Independent")),
  natlorigin = c(rep("Cuban"))
)



df6 <- tibble(
  Message = c(
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = olr_coef6[, 1],
  se = olr_coef6[, 2],
  pid = c(rep("Republican")),
  natlorigin = c(rep("Cuban"))
)



## Puerto Rican


df7 <- tibble(
  Message = c(
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = olr_coef7[, 1],
  se = olr_coef7[, 2],
  pid = c(rep("Democrat")),
  natlorigin = c(rep("Puerto Rican"))
)

df8 <- tibble(
  Message = c(
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = olr_coef8[, 1],
  se = olr_coef8[, 2],
  pid = c(rep("Independent")),
  natlorigin = c(rep("Puerto Rican"))
)



df9 <- tibble(
  Message = c(
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = olr_coef9[, 1],
  se = olr_coef9[, 2],
  pid = c(rep("Republican")),
  natlorigin = c(rep("Puerto Rican"))
)

demcand.df <- rbind(df1, df2, df3, df4, df5, df6, df7, df8, df9)

## Final dataclean

# Fixing factor levels

demcand.df$pid <- factor(demcand.df$pid,
  levels = c(
    "Democrat",
    "Independent",
    "Republican"
  )
)

demcand.df$Message <- factor(demcand.df$Message,
  levels = c(
    "Pro-Undocumented (D)",
    "Pro-Legal (D)",
    "Progressive Econ. (D)",
    "Moderate Econ. (D)"
  )
)



## Graph

demcand.plot <- ggplot(
  data = demcand.df,
  aes(
    x = Message,
    y = coefficient
  )
) +
  geom_point(
    aes(
      x = Message,
      y = coefficient,
      color = Message
    ),
    size = 4
  ) +
  geom_linerange(
    aes(
      x = Message,
      color = Message,
      ymin = coefficient - 1.96 * se,
      ymax = coefficient + 1.96 * se
    ),
    lwd = 2
  ) +
  facet_grid(natlorigin ~ pid) +
  coord_flip() +
  labs(
    x = "",
    y = "Estimated Effect of Treatment Assignment on Change in Treatment Ad Outcome (1-5)"
  ) +
  theme(
    legend.position = "none",
    text = element_text(size = 16)
  )

## Adding constant line at 0

demcand.plot + geom_hline(
  data = demcand.df,
  aes(yintercept = 0)
)

ggsave("Final/Appendix/study2_demcandfigure_bynatlorigin.png",
  height = 6,
  width = 12
)
```


```{r Study 2 Appendix Republican Candidate Message by PID x Natl Origin}
## Separate models for each party ID subgroup x Natl Origin


## Democratic candidate

## Significance tests
# Clustering SEs by respondent


### Mexican Natl Origin

# Democrat
df1 <- reppooled.df %>% filter(
  party_id == "Democrat",
  natlorigin_clean == "Mexican"
)

olr1 <- polr(dv_ad7_ord ~ t_7,
  data = df1,
  Hess = TRUE
)

olr_coef1 <- coeftest(olr1,
  vcov = vcovCL(olr1, factor(df1$ResponseId))
)


# Independent
df2 <- reppooled.df %>% filter(
  party_id %in% c(
    "Independent lean Dem",
    "Independent no lean",
    "Independent lean Rep"
  ),
  natlorigin_clean == "Mexican"
)

olr2 <- polr(dv_ad7_ord ~ t_7,
  data = df2,
  Hess = TRUE
)

olr_coef2 <- coeftest(olr2,
  vcov = vcovCL(olr2, factor(df2$ResponseId))
)

# Republican
df3 <- reppooled.df %>% filter(
  party_id == "Republican",
  natlorigin_clean == "Mexican"
)

olr3 <- polr(dv_ad7_ord ~ t_7,
  data = df3,
  Hess = TRUE
)

olr_coef3 <- coeftest(olr3,
  vcov = vcovCL(olr3, factor(df3$ResponseId))
)



### Cuban Natl Origin

# Democrat
df4 <- reppooled.df %>% filter(
  party_id == "Democrat",
  natlorigin_clean == "Cuban"
)

olr4 <- polr(dv_ad7_ord ~ t_7,
  data = df4,
  Hess = TRUE
)

olr_coef4 <- coeftest(olr4,
  vcov = vcovCL(olr4, factor(df4$ResponseId))
)


# Independent
df5 <- reppooled.df %>% filter(
  party_id %in% c(
    "Independent lean Dem",
    "Independent no lean",
    "Independent lean Rep"
  ),
  natlorigin_clean == "Cuban"
)

olr5 <- polr(dv_ad7_ord ~ t_7,
  data = df5,
  Hess = TRUE
)

olr_coef5 <- coeftest(olr5,
  vcov = vcovCL(olr5, factor(df5$ResponseId))
)

# Republican
df6 <- reppooled.df %>% filter(
  party_id == "Republican",
  natlorigin_clean == "Cuban"
)

olr6 <- polr(dv_ad7_ord ~ t_7,
  data = df6,
  Hess = TRUE
)

olr_coef6 <- coeftest(olr6,
  vcov = vcovCL(olr6, factor(df6$ResponseId))
)



### Puerto Rican Natl Origin

# Democrat
df7 <- reppooled.df %>% filter(
  party_id == "Democrat",
  natlorigin_clean == "Puerto Rican"
)

olr7 <- polr(dv_ad7_ord ~ t_7,
  data = df7,
  Hess = TRUE
)

olr_coef7 <- coeftest(olr7,
  vcov = vcovCL(olr7, factor(df7$ResponseId))
)


# Independent
df8 <- reppooled.df %>% filter(
  party_id %in% c(
    "Independent lean Dem",
    "Independent no lean",
    "Independent lean Rep"
  ),
  natlorigin_clean == "Puerto Rican"
)

olr8 <- polr(dv_ad7_ord ~ t_7,
  data = df8,
  Hess = TRUE
)

olr_coef8 <- coeftest(olr8,
  vcov = vcovCL(olr8, factor(df8$ResponseId))
)

# Republican
df9 <- reppooled.df %>% filter(
  party_id == "Republican",
  natlorigin_clean == "Puerto Rican"
)

olr9 <- polr(dv_ad7_ord ~ t_7,
  data = df9,
  Hess = TRUE
)

olr_coef9 <- coeftest(olr9,
  vcov = vcovCL(olr9, factor(df9$ResponseId))
)





## Job Talk Study 2 Democratic Candidate Figure x Natl Origin


## Mexican

df1 <- tibble(
  Message = c(
    "Moderate Econ. (R)",
    "Pro-Business Econ. (R)",
    "Pro-Legal (R)",
    "Anti-Undocumented (R)"
  ),
  coefficient = olr_coef1[, 1],
  se = olr_coef1[, 2],
  pid = c(rep("Democrat")),
  natlorigin = c(rep("Mexican"))
)

df2 <- tibble(
  Message = c(
    "Moderate Econ. (R)",
    "Pro-Business Econ. (R)",
    "Pro-Legal (R)",
    "Anti-Undocumented (R)"
  ),
  coefficient = olr_coef2[, 1],
  se = olr_coef2[, 2],
  pid = c(rep("Independent")),
  natlorigin = c(rep("Mexican"))
)



df3 <- tibble(
  Message = c(
    "Moderate Econ. (R)",
    "Pro-Business Econ. (R)",
    "Pro-Legal (R)",
    "Anti-Undocumented (R)"
  ),
  coefficient = olr_coef5[, 1],
  se = olr_coef3[, 2],
  pid = c(rep("Republican")),
  natlorigin = c(rep("Mexican"))
)


## Cuban


df4 <- tibble(
  Message = c(
    "Moderate Econ. (R)",
    "Pro-Business Econ. (R)",
    "Pro-Legal (R)",
    "Anti-Undocumented (R)"
  ),
  coefficient = olr_coef4[, 1],
  se = olr_coef4[, 2],
  pid = c(rep("Democrat")),
  natlorigin = c(rep("Cuban"))
)

df5 <- tibble(
  Message = c(
    "Moderate Econ. (R)",
    "Pro-Business Econ. (R)",
    "Pro-Legal (R)",
    "Anti-Undocumented (R)"
  ),
  coefficient = olr_coef5[, 1],
  se = olr_coef5[, 2],
  pid = c(rep("Independent")),
  natlorigin = c(rep("Cuban"))
)



df6 <- tibble(
  Message = c(
    "Moderate Econ. (R)",
    "Pro-Business Econ. (R)",
    "Pro-Legal (R)",
    "Anti-Undocumented (R)"
  ),
  coefficient = olr_coef6[, 1],
  se = olr_coef6[, 2],
  pid = c(rep("Republican")),
  natlorigin = c(rep("Cuban"))
)



## Puerto Rican


df7 <- tibble(
  Message = c(
    "Moderate Econ. (R)",
    "Pro-Business Econ. (R)",
    "Pro-Legal (R)",
    "Anti-Undocumented (R)"
  ),
  coefficient = olr_coef7[, 1],
  se = olr_coef7[, 2],
  pid = c(rep("Democrat")),
  natlorigin = c(rep("Puerto Rican"))
)

df8 <- tibble(
  Message = c(
    "Moderate Econ. (R)",
    "Pro-Business Econ. (R)",
    "Pro-Legal (R)",
    "Anti-Undocumented (R)"
  ),
  coefficient = olr_coef8[, 1],
  se = olr_coef8[, 2],
  pid = c(rep("Independent")),
  natlorigin = c(rep("Puerto Rican"))
)



df9 <- tibble(
  Message = c(
    "Moderate Econ. (R)",
    "Pro-Business Econ. (R)",
    "Pro-Legal (R)",
    "Anti-Undocumented (R)"
  ),
  coefficient = olr_coef9[, 1],
  se = olr_coef9[, 2],
  pid = c(rep("Republican")),
  natlorigin = c(rep("Puerto Rican"))
)

repcand.df <- rbind(df1, df2, df3, df4, df5, df6, df7, df8, df9)

## Final dataclean

# Fixing factor levels

repcand.df$pid <- factor(repcand.df$pid,
  levels = c(
    "Democrat",
    "Independent",
    "Republican"
  )
)

repcand.df$Message <- factor(repcand.df$Message,
  levels = c(
    "Anti-Undocumented (R)",
    "Pro-Legal (R)",
    "Pro-Business Econ. (R)",
    "Moderate Econ. (R)"
  )
)



## Graph

repcand.plot <- ggplot(
  data = repcand.df,
  aes(
    x = Message,
    y = coefficient
  )
) +
  geom_point(
    aes(
      x = Message,
      y = coefficient,
      color = Message
    ),
    size = 4
  ) +
  geom_linerange(
    aes(
      x = Message,
      color = Message,
      ymin = coefficient - 1.96 * se,
      ymax = coefficient + 1.96 * se
    ),
    lwd = 2
  ) +
  facet_grid(natlorigin ~ pid) +
  coord_flip() +
  labs(
    x = "",
    y = "Estimated Effect of Treatment Assignment on Change in Treatment Ad Outcome (1-5)"
  ) +
  theme(
    legend.position = "none",
    text = element_text(size = 16)
  )

## Adding constant line at 0

repcand.plot + geom_hline(
  data = repcand.df,
  aes(yintercept = 0)
)

ggsave("Final/Appendix/study2_repcandfigure_bynatlorigin.png",
  height = 6,
  width = 12
)
```



```{r Study 2 Appendix Democratic Candidate Message by PID x Race}
## Separate models for each party ID subgroup x Race


diag_df <- dempooled.df %>%
  group_by(race) %>%
  summarize(count = n())

## Moderate numbers in each subgroup

## Democratic candidate

## Significance tests
# Clustering SEs by respondent


### Only Latino

# Democrat
df1 <- dempooled.df %>% filter(
  party_id == "Democrat",
  race == "Only Latino/Hispanic")

olr1 <- polr(dv_ad2_ord ~ t_2,
  data = df1,
  Hess = TRUE
)

olr_coef1 <- coeftest(olr1,
  vcov = vcovCL(olr1, factor(df1$ResponseId))
)


# Independent
df2 <- dempooled.df %>% filter(
  party_id %in% c(
    "Independent lean Dem",
    "Independent no lean",
    "Independent lean Rep"
  ),
  race == "Only Latino/Hispanic"
)

olr2 <- polr(dv_ad2_ord ~ t_2,
  data = df2,
  Hess = TRUE
)

olr_coef2 <- coeftest(olr2,
  vcov = vcovCL(olr2, factor(df2$ResponseId))
)

# Republican
df3 <- dempooled.df %>% filter(
  party_id == "Republican",
  race == "Only Latino/Hispanic"
)

olr3 <- polr(dv_ad2_ord ~ t_2,
  data = df3,
  Hess = TRUE
)

olr_coef3 <- coeftest(olr3,
  vcov = vcovCL(olr3, factor(df3$ResponseId))
)



### Latino and White

# Democrat
df4 <- dempooled.df %>% filter(
  party_id == "Democrat",
  race == "Latino and White"
)

olr4 <- polr(dv_ad2_ord ~ t_2,
  data = df4,
  Hess = TRUE
)

olr_coef4 <- coeftest(olr4,
  vcov = vcovCL(olr4, factor(df4$ResponseId))
)


# Independent
df5 <- dempooled.df %>% filter(
  party_id %in% c(
    "Independent lean Dem",
    "Independent no lean",
    "Independent lean Rep"
  ),
  race == "Latino and White"
)

olr5 <- polr(dv_ad2_ord ~ t_2,
  data = df5,
  Hess = TRUE
)

olr_coef5 <- coeftest(olr5,
  vcov = vcovCL(olr5, factor(df5$ResponseId))
)

# Republican
df6 <- dempooled.df %>% filter(
  party_id == "Republican",
  race == "Latino and White"
)


olr6 <- polr(dv_ad2_ord ~ t_2,
  data = df6,
  Hess = TRUE
)

olr_coef6 <- coeftest(olr6,
  vcov = vcovCL(olr6, factor(df6$ResponseId))
)



### Latino and Black

# Democrat
df7 <- dempooled.df %>% filter(
  party_id == "Democrat",
  race == "Latino and Black"
)

olr7 <- polr(dv_ad2_ord ~ t_2,
  data = df7,
  Hess = TRUE
)

olr_coef7 <- coeftest(olr7,
  vcov = vcovCL(olr7, factor(df7$ResponseId))
)


# Independent
df8 <- dempooled.df %>% filter(
  party_id %in% c(
    "Independent lean Dem",
    "Independent no lean",
    "Independent lean Rep"
  ),
  race == "Latino and Black"
)

olr8 <- polr(dv_ad2_ord ~ t_2,
  data = df8,
  Hess = TRUE
)

olr_coef8 <- coeftest(olr8,
  vcov = vcovCL(olr8, factor(df8$ResponseId))
)

# Republican
df9 <- dempooled.df %>% filter(
  party_id == "Republican",
  race == "Latino and Black"
)

olr9 <- polr(dv_ad2_ord ~ t_2,
  data = df9,
  Hess = TRUE
)

olr_coef9 <- coeftest(olr9,
  vcov = vcovCL(olr9, factor(df9$ResponseId))
)

### Other/Multiracial

# Democrat
df10 <- dempooled.df %>% filter(
  party_id == "Democrat",
  race == "Other/Multiracial"
)

olr10 <- polr(dv_ad2_ord ~ t_2,
  data = df10,
  Hess = TRUE
)

olr_coef10 <- coeftest(olr10,
  vcov = vcovCL(olr10, factor(df10$ResponseId))
)


# Independent
df11 <- dempooled.df %>% filter(
  party_id %in% c(
    "Independent lean Dem",
    "Independent no lean",
    "Independent lean Rep"
  ),
  race == "Other/Multiracial"
)

olr11 <- polr(dv_ad2_ord ~ t_2,
  data = df11,
  Hess = TRUE
)

olr_coef11 <- coeftest(olr11,
  vcov = vcovCL(olr11, factor(df11$ResponseId))
)

# Republican
df12 <- dempooled.df %>% filter(
  party_id == "Republican",
  race == "Other/Multiracial"
)

olr12 <- polr(dv_ad2_ord ~ t_2,
  data = df12,
  Hess = TRUE
)

olr_coef12 <- coeftest(olr9,
  vcov = vcovCL(olr12, factor(df12$ResponseId))
)


## Only Latinos


df1 <- tibble(
  Message = c(
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = olr_coef1[, 1],
  se = olr_coef1[, 2],
  pid = c(rep("Democrat")),
  race = "Only Latino"
)

df2 <- tibble(
  Message = c(
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = olr_coef2[, 1],
  se = olr_coef2[, 2],
  pid = c(rep("Independent")),
  race = "Only Latino"
)



df3 <- tibble(
  Message = c(
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = olr_coef5[, 1],
  se = olr_coef3[, 2],
  pid = c(rep("Republican")),
  race = "Only Latino"
)


## Latino and White


df4 <- tibble(
  Message = c(
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = olr_coef4[, 1],
  se = olr_coef4[, 2],
  pid = c(rep("Democrat")),
  race = "Latino and White"
)

df5 <- tibble(
  Message = c(
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = olr_coef5[, 1],
  se = olr_coef5[, 2],
  pid = c(rep("Independent")),
  race = "Latino and White"
)



df6 <- tibble(
  Message = c(
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = olr_coef6[, 1],
  se = olr_coef6[, 2],
  pid = c(rep("Republican")),
  race = "Latino and White"
)



## Latino and Black


df7 <- tibble(
  Message = c(
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = olr_coef7[, 1],
  se = olr_coef7[, 2],
  pid = c(rep("Democrat")),
  race = "Latino and Black"
)

df8 <- tibble(
  Message = c(
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = olr_coef8[, 1],
  se = olr_coef8[, 2],
  pid = c(rep("Independent")),
  race = "Latino and Black"
)



df9 <- tibble(
  Message = c(
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = olr_coef9[, 1],
  se = olr_coef9[, 2],
  pid = c(rep("Republican")),
  race = "Latino and Black"
)


## Other/Multiracial


df10 <- tibble(
  Message = c(
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = olr_coef10[, 1],
  se = olr_coef10[, 2],
  pid = c(rep("Democrat")),
  race = "Other/Multiracial"
)

df11 <- tibble(
  Message = c(
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = olr_coef11[, 1],
  se = olr_coef11[, 2],
  pid = c(rep("Independent")),
  race = "Other/Multiracial"
)

df12 <- tibble(
  Message = c(
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = olr_coef12[, 1],
  se = olr_coef12[, 2],
  pid = c(rep("Republican")),
  race = "Other/Multiracial"
)

demcand.df <- rbind(df1, df2, df3, 
                    df4, df5, df6, 
                    df7, df8, df9,
                    df10, df11, df12)

## Very sparse in some areas, dropping outlier findings:

demcand.df <- demcand.df %>%
  mutate(
    se = ifelse(
      coefficient > 7,
      0,
      se
    ),
    coefficient = ifelse(
      coefficient > 7,
      0,
      coefficient
    )
  )

## Final dataclean

# Fixing factor levels

demcand.df$pid <- factor(demcand.df$pid,
  levels = c(
    "Democrat",
    "Independent",
    "Republican"
  )
)

demcand.df$Message <- factor(demcand.df$Message,
  levels = c(
    "Pro-Undocumented (D)",
    "Pro-Legal (D)",
    "Progressive Econ. (D)",
    "Moderate Econ. (D)"
  )
)


demcand.df$Race <- factor(demcand.df$race,
  levels = c(
    "Only Latino",
    "Latino and White",
    "Latino and Black",
    "Other/Multiracial"
  )
)



## Graph

demcand.plot <- ggplot(
  data = demcand.df,
  aes(
    x = Message,
    y = coefficient
  )
) +
  geom_point(
    aes(
      x = Message,
      y = coefficient,
      color = Message
    ),
    size = 4
  ) +
  geom_linerange(
    aes(
      x = Message,
      color = Message,
      ymin = coefficient - 1.96 * se,
      ymax = coefficient + 1.96 * se
    ),
    lwd = 2
  ) +
  facet_grid(Race ~ pid) +
  coord_flip() +
  labs(
    x = "",
    y = "Estimated Effect of Treatment Assignment on Change in Treatment Ad Outcome (1-5)"
  ) +
  theme(
    legend.position = "none",
    text = element_text(size = 16)
  )

## Adding constant line at 0

demcand.plot + geom_hline(
  data = demcand.df,
  aes(yintercept = 0)
)

ggsave("Final/Appendix/study2_demcandfigure_byrace.png",
  height = 10,
  width = 12
)
```


```{r Study 2 Appendix Republican Candidate Message by PID x Race}
## Separate models for each party ID subgroup x Race


diag_df <- reppooled.df %>%
  group_by(race) %>%
  summarize(count = n())

## Moderate numbers in each subgroup

## Democratic candidate

## Significance tests
# Clustering SEs by respondent


### Only Latino

# Democrat
df1 <- reppooled.df %>% filter(
  party_id == "Democrat",
  race == "Only Latino/Hispanic")

olr1 <- polr(dv_ad7_ord ~ t_7,
  data = df1,
  Hess = TRUE
)

olr_coef1 <- coeftest(olr1,
  vcov = vcovCL(olr1, factor(df1$ResponseId))
)


# Independent
df2 <- reppooled.df %>% filter(
  party_id %in% c(
    "Independent lean Dem",
    "Independent no lean",
    "Independent lean Rep"
  ),
  race == "Only Latino/Hispanic"
)

olr2 <- polr(dv_ad7_ord ~ t_7,
  data = df2,
  Hess = TRUE
)

olr_coef2 <- coeftest(olr2,
  vcov = vcovCL(olr2, factor(df2$ResponseId))
)

# Republican
df3 <- reppooled.df %>% filter(
  party_id == "Republican",
  race == "Only Latino/Hispanic"
)

olr3 <- polr(dv_ad7_ord ~ t_7,
  data = df3,
  Hess = TRUE
)

olr_coef3 <- coeftest(olr3,
  vcov = vcovCL(olr3, factor(df3$ResponseId))
)



### Latino and White

# Democrat
df4 <- reppooled.df %>% filter(
  party_id == "Democrat",
  race == "Latino and White"
)

olr4 <- polr(dv_ad7_ord ~ t_7,
  data = df4,
  Hess = TRUE
)

olr_coef4 <- coeftest(olr4,
  vcov = vcovCL(olr4, factor(df4$ResponseId))
)


# Independent
df5 <- reppooled.df %>% filter(
  party_id %in% c(
    "Independent lean Dem",
    "Independent no lean",
    "Independent lean Rep"
  ),
  race == "Latino and White"
)

olr5 <- polr(dv_ad7_ord ~ t_7,
  data = df5,
  Hess = TRUE
)

olr_coef5 <- coeftest(olr5,
  vcov = vcovCL(olr5, factor(df5$ResponseId))
)

# Republican
df6 <- reppooled.df %>% filter(
  party_id == "Republican",
  race == "Latino and White"
)


olr6 <- polr(dv_ad7_ord ~ t_7,
  data = df6,
  Hess = TRUE
)

olr_coef6 <- coeftest(olr6,
  vcov = vcovCL(olr6, factor(df6$ResponseId))
)



### Latino and Black

# Democrat
df7 <- reppooled.df %>% filter(
  party_id == "Democrat",
  race == "Latino and Black"
)

olr7 <- polr(dv_ad7_ord ~ t_7,
  data = df7,
  Hess = TRUE
)

olr_coef7 <- coeftest(olr7,
  vcov = vcovCL(olr7, factor(df7$ResponseId))
)


# Independent
df8 <- reppooled.df %>% filter(
  party_id %in% c(
    "Independent lean Dem",
    "Independent no lean",
    "Independent lean Rep"
  ),
  race == "Latino and Black"
)

olr8 <- polr(dv_ad7_ord ~ t_7,
  data = df8,
  Hess = TRUE
)

olr_coef8 <- coeftest(olr8,
  vcov = vcovCL(olr8, factor(df8$ResponseId))
)

# Republican
df9 <- reppooled.df %>% filter(
  party_id == "Republican",
  race == "Latino and Black"
)

## Rank-deficient

# olr9 <- polr(dv_ad7_ord ~ t_7,
#   data = df9,
#   Hess = TRUE
# )
# 
# olr_coef9 <- coeftest(olr9,
#   vcov = vcovCL(olr9, factor(df9$ResponseId))
# )

### Other/Multiracial

# Democrat
df10 <- reppooled.df %>% filter(
  party_id == "Democrat",
  race == "Other/Multiracial"
)

olr10 <- polr(dv_ad7_ord ~ t_7,
  data = df10,
  Hess = TRUE
)

olr_coef10 <- coeftest(olr10,
  vcov = vcovCL(olr10, factor(df10$ResponseId))
)


# Independent
df11 <- reppooled.df %>% filter(
  party_id %in% c(
    "Independent lean Dem",
    "Independent no lean",
    "Independent lean Rep"
  ),
  race == "Other/Multiracial"
)

olr11 <- polr(dv_ad7_ord ~ t_7,
  data = df11,
  Hess = TRUE
)

olr_coef11 <- coeftest(olr11,
  vcov = vcovCL(olr11, factor(df11$ResponseId))
)

# Republican
df12 <- reppooled.df %>% filter(
  party_id == "Republican",
  race == "Other/Multiracial"
)

olr12 <- polr(dv_ad7_ord ~ t_7,
  data = df12,
  Hess = TRUE
)

olr_coef12 <- coeftest(olr9,
  vcov = vcovCL(olr12, factor(df12$ResponseId))
)


## Only Latinos


df1 <- tibble(
  Message = c(
    "Moderate Econ. (R)",
    "Pro-Business Econ. (R)",
    "Pro-Legal (R)",
    "Anti-Undocumented (R)"
  ),
  coefficient = olr_coef1[, 1],
  se = olr_coef1[, 2],
  pid = c(rep("Democrat")),
  race = "Only Latino"
)

df2 <- tibble(
  Message = c(
    "Moderate Econ. (R)",
    "Pro-Business Econ. (R)",
    "Pro-Legal (R)",
    "Anti-Undocumented (R)"
  ),
  coefficient = olr_coef2[, 1],
  se = olr_coef2[, 2],
  pid = c(rep("Independent")),
  race = "Only Latino"
)



df3 <- tibble(
  Message = c(
    "Moderate Econ. (R)",
    "Pro-Business Econ. (R)",
    "Pro-Legal (R)",
    "Anti-Undocumented (R)"
  ),
  coefficient = olr_coef5[, 1],
  se = olr_coef3[, 2],
  pid = c(rep("Republican")),
  race = "Only Latino"
)


## Latino and White


df4 <- tibble(
  Message = c(
    "Moderate Econ. (R)",
    "Pro-Business Econ. (R)",
    "Pro-Legal (R)",
    "Anti-Undocumented (R)"
  ),
  coefficient = olr_coef4[, 1],
  se = olr_coef4[, 2],
  pid = c(rep("Democrat")),
  race = "Latino and White"
)

df5 <- tibble(
  Message = c(
    "Moderate Econ. (R)",
    "Pro-Business Econ. (R)",
    "Pro-Legal (R)",
    "Anti-Undocumented (R)"
  ),
  coefficient = olr_coef5[, 1],
  se = olr_coef5[, 2],
  pid = c(rep("Independent")),
  race = "Latino and White"
)



df6 <- tibble(
  Message = c(
    "Moderate Econ. (R)",
    "Pro-Business Econ. (R)",
    "Pro-Legal (R)",
    "Anti-Undocumented (R)"
  ),
  coefficient = olr_coef6[, 1],
  se = olr_coef6[, 2],
  pid = c(rep("Republican")),
  race = "Latino and White"
)



## Latino and Black


df7 <- tibble(
  Message = c(
    "Moderate Econ. (R)",
    "Pro-Business Econ. (R)",
    "Pro-Legal (R)",
    "Anti-Undocumented (R)"
  ),
  coefficient = olr_coef7[, 1],
  se = olr_coef7[, 2],
  pid = c(rep("Democrat")),
  race = "Latino and Black"
)

df8 <- tibble(
  Message = c(
    "Moderate Econ. (R)",
    "Pro-Business Econ. (R)",
    "Pro-Legal (R)",
    "Anti-Undocumented (R)"
  ),
  coefficient = olr_coef8[, 1],
  se = olr_coef8[, 2],
  pid = c(rep("Independent")),
  race = "Latino and Black"
)



df9 <- tibble(
  Message = c(
    "Moderate Econ. (R)",
    "Pro-Business Econ. (R)",
    "Pro-Legal (R)",
    "Anti-Undocumented (R)"
  ),
  coefficient = 0,
  se = 0,
  pid = c(rep("Republican")),
  race = "Latino and Black"
)


## Other/Multiracial


df10 <- tibble(
  Message = c(
    "Moderate Econ. (R)",
    "Pro-Business Econ. (R)",
    "Pro-Legal (R)",
    "Anti-Undocumented (R)"
  ),
  coefficient = olr_coef10[, 1],
  se = olr_coef10[, 2],
  pid = c(rep("Democrat")),
  race = "Other/Multiracial"
)

df11 <- tibble(
  Message = c(
    "Moderate Econ. (R)",
    "Pro-Business Econ. (R)",
    "Pro-Legal (R)",
    "Anti-Undocumented (R)"
  ),
  coefficient = olr_coef11[, 1],
  se = olr_coef11[, 2],
  pid = c(rep("Independent")),
  race = "Other/Multiracial"
)

df12 <- tibble(
  Message = c(
    "Moderate Econ. (R)",
    "Pro-Business Econ. (R)",
    "Pro-Legal (R)",
    "Anti-Undocumented (R)"
  ),
  coefficient = 0,
  se = 0,
  pid = c(rep("Republican")),
  race = "Other/Multiracial"
)

repcand.df <- rbind(df1, df2, df3, 
                    df4, df5, df6, 
                    df7, df8, df9,
                    df10, df11, df12)

## Very sparse in some areas, dropping outlier findings:

repcand.df <- repcand.df %>%
  mutate(
    se = ifelse(
      coefficient > 7,
      0,
      se
    ),
    coefficient = ifelse(
      coefficient > 7,
      0,
      coefficient
    )
  )

## Final dataclean

# Fixing factor levels

repcand.df$pid <- factor(repcand.df$pid,
  levels = c(
    "Democrat",
    "Independent",
    "Republican"
  )
)

repcand.df$Message <- factor(repcand.df$Message,
  levels = c(
    "Anti-Undocumented (R)",
    "Pro-Legal (R)",
    "Pro-Business Econ. (R)",
    "Moderate Econ. (R)"
  )
)


repcand.df$Race <- factor(repcand.df$race,
  levels = c(
    "Only Latino",
    "Latino and White",
    "Latino and Black",
    "Other/Multiracial"
  )
)



## Graph

repcand.plot <- ggplot(
  data = repcand.df,
  aes(
    x = Message,
    y = coefficient
  )
) +
  geom_point(
    aes(
      x = Message,
      y = coefficient,
      color = Message
    ),
    size = 4
  ) +
  geom_linerange(
    aes(
      x = Message,
      color = Message,
      ymin = coefficient - 1.96 * se,
      ymax = coefficient + 1.96 * se
    ),
    lwd = 2
  ) +
  facet_grid(Race ~ pid) +
  coord_flip() +
  labs(
    x = "",
    y = "Estimated Effect of Treatment Assignment on Change in Treatment Ad Outcome (1-5)"
  ) +
  theme(
    legend.position = "none",
    text = element_text(size = 16)
  )

## Adding constant line at 0

repcand.plot + geom_hline(
  data = repcand.df,
  aes(yintercept = 0)
)

ggsave("Final/Appendix/study2_repcandfigure_byrace.png",
  height = 10,
  width = 12
)
```




```{r Study 2 Appendix Democratic Candidate Message by PID x Imm Gen}
## Separate models for each party ID subgroup x Natl Origin


## Democratic candidate

## Significance tests
# Clustering SEs by respondent


### 1st Natl Origin

# Democrat
df1 <- dempooled.df %>% filter(
  party_id == "Democrat",
  imm_gen == "1st"
)

olr1 <- polr(dv_ad2_ord ~ t_2,
  data = df1,
  Hess = TRUE
)

olr_coef1 <- coeftest(olr1,
  vcov = vcovCL(olr1, factor(df1$ResponseId))
)


# Independent
df2 <- dempooled.df %>% filter(
  party_id %in% c(
    "Independent lean Dem",
    "Independent no lean",
    "Independent lean Rep"
  ),
  imm_gen == "1st"
)

olr2 <- polr(dv_ad2_ord ~ t_2,
  data = df2,
  Hess = TRUE
)

olr_coef2 <- coeftest(olr2,
  vcov = vcovCL(olr2, factor(df2$ResponseId))
)

# Republican
df3 <- dempooled.df %>% filter(
  party_id == "Republican",
  imm_gen == "1st"
)

olr3 <- polr(dv_ad2_ord ~ t_2,
  data = df3,
  Hess = TRUE
)

olr_coef3 <- coeftest(olr3,
  vcov = vcovCL(olr3, factor(df3$ResponseId))
)



### 2nd Natl Origin

# Democrat
df4 <- dempooled.df %>% filter(
  party_id == "Democrat",
  imm_gen == "2nd"
)

olr4 <- polr(dv_ad2_ord ~ t_2,
  data = df4,
  Hess = TRUE
)

olr_coef4 <- coeftest(olr4,
  vcov = vcovCL(olr4, factor(df4$ResponseId))
)


# Independent
df5 <- dempooled.df %>% filter(
  party_id %in% c(
    "Independent lean Dem",
    "Independent no lean",
    "Independent lean Rep"
  ),
  imm_gen == "2nd"
)

olr5 <- polr(dv_ad2_ord ~ t_2,
  data = df5,
  Hess = TRUE
)

olr_coef5 <- coeftest(olr5,
  vcov = vcovCL(olr5, factor(df5$ResponseId))
)

# Republican
df6 <- dempooled.df %>% filter(
  party_id == "Republican",
  imm_gen == "2nd"
)

olr6 <- polr(dv_ad2_ord ~ t_2,
  data = df6,
  Hess = TRUE
)

olr_coef6 <- coeftest(olr6,
  vcov = vcovCL(olr6, factor(df6$ResponseId))
)



### 3rd+ Natl Origin

# Democrat
df7 <- dempooled.df %>% filter(
  party_id == "Democrat",
  imm_gen == "3rd+"
)

olr7 <- polr(dv_ad2_ord ~ t_2,
  data = df7,
  Hess = TRUE
)

olr_coef7 <- coeftest(olr7,
  vcov = vcovCL(olr7, factor(df7$ResponseId))
)


# Independent
df8 <- dempooled.df %>% filter(
  party_id %in% c(
    "Independent lean Dem",
    "Independent no lean",
    "Independent lean Rep"
  ),
  imm_gen == "3rd+"
)

olr8 <- polr(dv_ad2_ord ~ t_2,
  data = df8,
  Hess = TRUE
)

olr_coef8 <- coeftest(olr8,
  vcov = vcovCL(olr8, factor(df8$ResponseId))
)

# Republican
df9 <- dempooled.df %>% filter(
  party_id == "Republican",
  imm_gen == "3rd+"
)

olr9 <- polr(dv_ad2_ord ~ t_2,
  data = df9,
  Hess = TRUE
)

olr_coef9 <- coeftest(olr9,
  vcov = vcovCL(olr9, factor(df9$ResponseId))
)





## Job Talk Study 2 Democratic Candidate Figure x Natl Origin


## 1st

df1 <- tibble(
  Message = c(
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = olr_coef1[, 1],
  se = olr_coef1[, 2],
  pid = c(rep("Democrat")),
  imm_gen = c(rep("1st"))
)

df2 <- tibble(
  Message = c(
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = olr_coef2[, 1],
  se = olr_coef2[, 2],
  pid = c(rep("Independent")),
  imm_gen = c(rep("1st"))
)



df3 <- tibble(
  Message = c(
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = olr_coef5[, 1],
  se = olr_coef3[, 2],
  pid = c(rep("Republican")),
  imm_gen = c(rep("1st"))
)


## 2nd


df4 <- tibble(
  Message = c(
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = olr_coef4[, 1],
  se = olr_coef4[, 2],
  pid = c(rep("Democrat")),
  imm_gen = c(rep("2nd"))
)

df5 <- tibble(
  Message = c(
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = olr_coef5[, 1],
  se = olr_coef5[, 2],
  pid = c(rep("Independent")),
  imm_gen = c(rep("2nd"))
)



df6 <- tibble(
  Message = c(
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = olr_coef6[, 1],
  se = olr_coef6[, 2],
  pid = c(rep("Republican")),
  imm_gen = c(rep("2nd"))
)



## 3rd+


df7 <- tibble(
  Message = c(
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = olr_coef7[, 1],
  se = olr_coef7[, 2],
  pid = c(rep("Democrat")),
  imm_gen = c(rep("3rd+"))
)

df8 <- tibble(
  Message = c(
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = olr_coef8[, 1],
  se = olr_coef8[, 2],
  pid = c(rep("Independent")),
  imm_gen = c(rep("3rd+"))
)



df9 <- tibble(
  Message = c(
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = olr_coef9[, 1],
  se = olr_coef9[, 2],
  pid = c(rep("Republican")),
  imm_gen = c(rep("3rd+"))
)

demcand.df <- rbind(df1, df2, df3, df4, df5, df6, df7, df8, df9)

## Final dataclean

# Fixing factor levels

demcand.df$pid <- factor(demcand.df$pid,
  levels = c(
    "Democrat",
    "Independent",
    "Republican"
  )
)

demcand.df$Message <- factor(demcand.df$Message,
  levels = c(
    "Pro-Undocumented (D)",
    "Pro-Legal (D)",
    "Progressive Econ. (D)",
    "Moderate Econ. (D)"
  )
)



## Graph

demcand.plot <- ggplot(
  data = demcand.df,
  aes(
    x = Message,
    y = coefficient
  )
) +
  geom_point(
    aes(
      x = Message,
      y = coefficient,
      color = Message
    ),
    size = 4
  ) +
  geom_linerange(
    aes(
      x = Message,
      color = Message,
      ymin = coefficient - 1.96 * se,
      ymax = coefficient + 1.96 * se
    ),
    lwd = 2
  ) +
  facet_grid(imm_gen ~ pid) +
  coord_flip() +
  labs(
    x = "",
    y = "Estimated Effect of Treatment Assignment on Change in Treatment Ad Outcome (1-5)"
  ) +
  theme(
    legend.position = "none",
    text = element_text(size = 16)
  )

## Adding constant line at 0

demcand.plot + geom_hline(
  data = demcand.df,
  aes(yintercept = 0)
)

ggsave("Final/Appendix/study2_demcandfigure_byimm_gen.png",
  height = 6,
  width = 12
)
```



```{r Study 2 Appendix Republican Candidate Message by PID x Imm Gen}
## Separate models for each party ID subgroup x Gen


## Republican

## Significance tests
# Clustering SEs by respondent


### 1st Gen

# Democrat
df1 <- reppooled.df %>% filter(
  party_id == "Democrat",
  imm_gen == "1st"
)

olr1 <- polr(dv_ad7_ord ~ t_7,
  data = df1,
  Hess = TRUE
)

olr_coef1 <- coeftest(olr1,
  vcov = vcovCL(olr1, factor(df1$ResponseId))
)


# Independent
df2 <- reppooled.df %>% filter(
  party_id %in% c(
    "Independent lean Dem",
    "Independent no lean",
    "Independent lean Rep"
  ),
  imm_gen == "1st"
)

olr2 <- polr(dv_ad7_ord ~ t_7,
  data = df2,
  Hess = TRUE
)

olr_coef2 <- coeftest(olr2,
  vcov = vcovCL(olr2, factor(df2$ResponseId))
)

# Republican
df3 <- reppooled.df %>% filter(
  party_id == "Republican",
  imm_gen == "1st"
)

olr3 <- polr(dv_ad7_ord ~ t_7,
  data = df3,
  Hess = TRUE
)

olr_coef3 <- coeftest(olr3,
  vcov = vcovCL(olr3, factor(df3$ResponseId))
)



### 2nd Gen

# Democrat
df4 <- reppooled.df %>% filter(
  party_id == "Democrat",
  imm_gen == "2nd"
)

olr4 <- polr(dv_ad7_ord ~ t_7,
  data = df4,
  Hess = TRUE
)

olr_coef4 <- coeftest(olr4,
  vcov = vcovCL(olr4, factor(df4$ResponseId))
)


# Independent
df5 <- reppooled.df %>% filter(
  party_id %in% c(
    "Independent lean Dem",
    "Independent no lean",
    "Independent lean Rep"
  ),
  imm_gen == "2nd"
)

olr5 <- polr(dv_ad7_ord ~ t_7,
  data = df5,
  Hess = TRUE
)

olr_coef5 <- coeftest(olr5,
  vcov = vcovCL(olr5, factor(df5$ResponseId))
)

# Republican
df6 <- reppooled.df %>% filter(
  party_id == "Republican",
  imm_gen == "2nd"
)

olr6 <- polr(dv_ad7_ord ~ t_7,
  data = df6,
  Hess = TRUE
)

olr_coef6 <- coeftest(olr6,
  vcov = vcovCL(olr6, factor(df6$ResponseId))
)



### 3rd+ Gen

# Democrat
df7 <- reppooled.df %>% filter(
  party_id == "Democrat",
  imm_gen == "3rd+"
)

olr7 <- polr(dv_ad7_ord ~ t_7,
  data = df7,
  Hess = TRUE
)

olr_coef7 <- coeftest(olr7,
  vcov = vcovCL(olr7, factor(df7$ResponseId))
)


# Independent
df8 <- reppooled.df %>% filter(
  party_id %in% c(
    "Independent lean Dem",
    "Independent no lean",
    "Independent lean Rep"
  ),
  imm_gen == "3rd+"
)

olr8 <- polr(dv_ad7_ord ~ t_7,
  data = df8,
  Hess = TRUE
)

olr_coef8 <- coeftest(olr8,
  vcov = vcovCL(olr8, factor(df8$ResponseId))
)

# Republican
df9 <- reppooled.df %>% filter(
  party_id == "Republican",
  imm_gen == "3rd+"
)

olr9 <- polr(dv_ad7_ord ~ t_7,
  data = df9,
  Hess = TRUE
)

olr_coef9 <- coeftest(olr9,
  vcov = vcovCL(olr9, factor(df9$ResponseId))
)





## Job Talk Study 2 Republican Candidate Figure x Gen


## 1st

df1 <- tibble(
  Message = c(
    "Moderate Econ. (R)",
    "Pro-Business Econ. (R)",
    "Pro-Legal (R)",
    "Anti-Undocumented (R)"
  ),
  coefficient = olr_coef1[, 1],
  se = olr_coef1[, 2],
  pid = c(rep("Democrat")),
  imm_gen = c(rep("1st"))
)

df2 <- tibble(
  Message = c(
    "Moderate Econ. (R)",
    "Pro-Business Econ. (R)",
    "Pro-Legal (R)",
    "Anti-Undocumented (R)"
  ),
  coefficient = olr_coef2[, 1],
  se = olr_coef2[, 2],
  pid = c(rep("Independent")),
  imm_gen = c(rep("1st"))
)



df3 <- tibble(
  Message = c(
    "Moderate Econ. (R)",
    "Pro-Business Econ. (R)",
    "Pro-Legal (R)",
    "Anti-Undocumented (R)"
  ),
  coefficient = olr_coef5[, 1],
  se = olr_coef3[, 2],
  pid = c(rep("Republican")),
  imm_gen = c(rep("1st"))
)


## 2nd


df4 <- tibble(
  Message = c(
    "Moderate Econ. (R)",
    "Pro-Business Econ. (R)",
    "Pro-Legal (R)",
    "Anti-Undocumented (R)"
  ),
  coefficient = olr_coef4[, 1],
  se = olr_coef4[, 2],
  pid = c(rep("Democrat")),
  imm_gen = c(rep("2nd"))
)

df5 <- tibble(
  Message = c(
    "Moderate Econ. (R)",
    "Pro-Business Econ. (R)",
    "Pro-Legal (R)",
    "Anti-Undocumented (R)"
  ),
  coefficient = olr_coef5[, 1],
  se = olr_coef5[, 2],
  pid = c(rep("Independent")),
  imm_gen = c(rep("2nd"))
)



df6 <- tibble(
  Message = c(
    "Moderate Econ. (R)",
    "Pro-Business Econ. (R)",
    "Pro-Legal (R)",
    "Anti-Undocumented (R)"
  ),
  coefficient = olr_coef6[, 1],
  se = olr_coef6[, 2],
  pid = c(rep("Republican")),
  imm_gen = c(rep("2nd"))
)



## 3rd+


df7 <- tibble(
  Message = c(
    "Moderate Econ. (R)",
    "Pro-Business Econ. (R)",
    "Pro-Legal (R)",
    "Anti-Undocumented (R)"
  ),
  coefficient = olr_coef7[, 1],
  se = olr_coef7[, 2],
  pid = c(rep("Democrat")),
  imm_gen = c(rep("3rd+"))
)

df8 <- tibble(
  Message = c(
    "Moderate Econ. (R)",
    "Pro-Business Econ. (R)",
    "Pro-Legal (R)",
    "Anti-Undocumented (R)"
  ),
  coefficient = olr_coef8[, 1],
  se = olr_coef8[, 2],
  pid = c(rep("Independent")),
  imm_gen = c(rep("3rd+"))
)



df9 <- tibble(
  Message = c(
    "Moderate Econ. (R)",
    "Pro-Business Econ. (R)",
    "Pro-Legal (R)",
    "Anti-Undocumented (R)"
  ),
  coefficient = olr_coef9[, 1],
  se = olr_coef9[, 2],
  pid = c(rep("Republican")),
  imm_gen = c(rep("3rd+"))
)

demcand.df <- rbind(df1, df2, df3, df4, df5, df6, df7, df8, df9)

## Final dataclean

# Fixing factor levels

demcand.df$pid <- factor(demcand.df$pid,
  levels = c(
    "Democrat",
    "Independent",
    "Republican"
  )
)

demcand.df$Message <- factor(demcand.df$Message,
  levels = c(
    "Anti-Undocumented (R)",
    "Pro-Legal (R)",
    "Pro-Business Econ. (R)",
    "Moderate Econ. (R)"
  )
)



## Graph

demcand.plot <- ggplot(
  data = demcand.df,
  aes(
    x = Message,
    y = coefficient
  )
) +
  geom_point(
    aes(
      x = Message,
      y = coefficient,
      color = Message
    ),
    size = 4
  ) +
  geom_linerange(
    aes(
      x = Message,
      color = Message,
      ymin = coefficient - 1.96 * se,
      ymax = coefficient + 1.96 * se
    ),
    lwd = 2
  ) +
  facet_grid(imm_gen ~ pid) +
  coord_flip() +
  labs(
    x = "",
    y = "Estimated Effect of Treatment Assignment on Change in Treatment Ad Outcome (1-5)"
  ) +
  theme(
    legend.position = "none",
    text = element_text(size = 16)
  )

## Adding constant line at 0

demcand.plot + geom_hline(
  data = demcand.df,
  aes(yintercept = 0)
)

ggsave("Final/Appendix/study2_repcandfigure_byimm_gen.png",
  height = 6,
  width = 12
)
```




```{r Study 2 Appendix Democratic Candidate Message by PID x # Undoc Known}
## Separate models for each party ID subgroup x # Undoc Known


dempooled.df %>%
  group_by(undoc_know) %>%
  summarize(count = n())

## About equal numbers in:
## A lot/Some (900)
## A few (1200)
## None (1050)


## Democratic candidate

## Significance tests
# Clustering SEs by respondent


### A lot/Some

# Democrat
df1 <- dempooled.df %>% filter(
  party_id == "Democrat",
  undoc_know %in% c("A lot", "Some")
)

olr1 <- polr(dv_ad2_ord ~ t_2,
  data = df1,
  Hess = TRUE
)

olr_coef1 <- coeftest(olr1,
  vcov = vcovCL(olr1, factor(df1$ResponseId))
)


# Independent
df2 <- dempooled.df %>% filter(
  party_id %in% c(
    "Independent lean Dem",
    "Independent no lean",
    "Independent lean Rep"
  ),
  undoc_know %in% c("A lot", "Some")
)

olr2 <- polr(dv_ad2_ord ~ t_2,
  data = df2,
  Hess = TRUE
)

olr_coef2 <- coeftest(olr2,
  vcov = vcovCL(olr2, factor(df2$ResponseId))
)

# Republican
df3 <- dempooled.df %>% filter(
  party_id == "Republican",
  undoc_know %in% c("A lot", "Some")
)

olr3 <- polr(dv_ad2_ord ~ t_2,
  data = df3,
  Hess = TRUE
)

olr_coef3 <- coeftest(olr3,
  vcov = vcovCL(olr3, factor(df3$ResponseId))
)



### A few

# Democrat
df4 <- dempooled.df %>% filter(
  party_id == "Democrat",
  undoc_know == "A few"
)

olr4 <- polr(dv_ad2_ord ~ t_2,
  data = df4,
  Hess = TRUE
)

olr_coef4 <- coeftest(olr4,
  vcov = vcovCL(olr4, factor(df4$ResponseId))
)


# Independent
df5 <- dempooled.df %>% filter(
  party_id %in% c(
    "Independent lean Dem",
    "Independent no lean",
    "Independent lean Rep"
  ),
  undoc_know == "A few"
)

olr5 <- polr(dv_ad2_ord ~ t_2,
  data = df5,
  Hess = TRUE
)

olr_coef5 <- coeftest(olr5,
  vcov = vcovCL(olr5, factor(df5$ResponseId))
)

# Republican
df6 <- dempooled.df %>% filter(
  party_id == "Republican",
  undoc_know == "A few"
)

olr6 <- polr(dv_ad2_ord ~ t_2,
  data = df6,
  Hess = TRUE
)

olr_coef6 <- coeftest(olr6,
  vcov = vcovCL(olr6, factor(df6$ResponseId))
)



### None

# Democrat
df7 <- dempooled.df %>% filter(
  party_id == "Democrat",
  undoc_know == "None"
)

olr7 <- polr(dv_ad2_ord ~ t_2,
  data = df7,
  Hess = TRUE
)

olr_coef7 <- coeftest(olr7,
  vcov = vcovCL(olr7, factor(df7$ResponseId))
)


# Independent
df8 <- dempooled.df %>% filter(
  party_id %in% c(
    "Independent lean Dem",
    "Independent no lean",
    "Independent lean Rep"
  ),
  undoc_know == "None"
)

olr8 <- polr(dv_ad2_ord ~ t_2,
  data = df8,
  Hess = TRUE
)

olr_coef8 <- coeftest(olr8,
  vcov = vcovCL(olr8, factor(df8$ResponseId))
)

# Republican
df9 <- dempooled.df %>% filter(
  party_id == "Republican",
  undoc_know == "None"
)

olr9 <- polr(dv_ad2_ord ~ t_2,
  data = df9,
  Hess = TRUE
)

olr_coef9 <- coeftest(olr9,
  vcov = vcovCL(olr9, factor(df9$ResponseId))
)





## Job Talk Study 2 Democratic Candidate Figure x Natl Origin


## A lot/Some

df1 <- tibble(
  Message = c(
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = olr_coef1[, 1],
  se = olr_coef1[, 2],
  pid = c(rep("Democrat")),
  undoc_know = c(rep("A lot/Some"))
)

df2 <- tibble(
  Message = c(
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = olr_coef2[, 1],
  se = olr_coef2[, 2],
  pid = c(rep("Independent")),
  undoc_know = c(rep("A lot/Some"))
)



df3 <- tibble(
  Message = c(
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = olr_coef5[, 1],
  se = olr_coef3[, 2],
  pid = c(rep("Republican")),
  undoc_know = c(rep("A lot/Some"))
)


## A few


df4 <- tibble(
  Message = c(
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = olr_coef4[, 1],
  se = olr_coef4[, 2],
  pid = c(rep("Democrat")),
  undoc_know = c(rep("A few"))
)

df5 <- tibble(
  Message = c(
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = olr_coef5[, 1],
  se = olr_coef5[, 2],
  pid = c(rep("Independent")),
  undoc_know = c(rep("A few"))
)



df6 <- tibble(
  Message = c(
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = olr_coef6[, 1],
  se = olr_coef6[, 2],
  pid = c(rep("Republican")),
  undoc_know = c(rep("A few"))
)



## 3rd+


df7 <- tibble(
  Message = c(
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = olr_coef7[, 1],
  se = olr_coef7[, 2],
  pid = c(rep("Democrat")),
  undoc_know = c(rep("None"))
)

df8 <- tibble(
  Message = c(
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = olr_coef8[, 1],
  se = olr_coef8[, 2],
  pid = c(rep("Independent")),
  undoc_know = c(rep("None"))
)



df9 <- tibble(
  Message = c(
    "Moderate Econ. (D)",
    "Progressive Econ. (D)",
    "Pro-Legal (D)",
    "Pro-Undocumented (D)"
  ),
  coefficient = olr_coef9[, 1],
  se = olr_coef9[, 2],
  pid = c(rep("Republican")),
  undoc_know = c(rep("None"))
)

demcand.df <- rbind(df1, df2, df3, df4, df5, df6, df7, df8, df9)

## Final dataclean

# Fixing factor levels

demcand.df$pid <- factor(demcand.df$pid,
  levels = c(
    "Democrat",
    "Independent",
    "Republican"
  )
)

demcand.df$Message <- factor(demcand.df$Message,
  levels = c(
    "Pro-Undocumented (D)",
    "Pro-Legal (D)",
    "Progressive Econ. (D)",
    "Moderate Econ. (D)"
  )
)


demcand.df$undoc_know <- factor(demcand.df$undoc_know,
  levels = c(
    "A lot/Some",
    "A few",
    "None"
  )
)



## Graph

demcand.plot <- ggplot(
  data = demcand.df,
  aes(
    x = Message,
    y = coefficient
  )
) +
  geom_point(
    aes(
      x = Message,
      y = coefficient,
      color = Message
    ),
    size = 4
  ) +
  geom_linerange(
    aes(
      x = Message,
      color = Message,
      ymin = coefficient - 1.96 * se,
      ymax = coefficient + 1.96 * se
    ),
    lwd = 2
  ) +
  facet_grid(undoc_know ~ pid) +
  coord_flip() +
  labs(
    x = "",
    y = "Estimated Effect of Treatment Assignment on Change in Treatment Ad Outcome (1-5)"
  ) +
  theme(
    legend.position = "none",
    text = element_text(size = 16)
  )

## Adding constant line at 0

demcand.plot + geom_hline(
  data = demcand.df,
  aes(yintercept = 0)
)

ggsave("Final/Appendix/study2_demcandfigure_byundoc_know.png",
  height = 6,
  width = 12
)
```



```{r Study 2 Appendix Republican Candidate Message by PID x Undoc Known}
## Separate models for each party ID subgroup x Gen


## Republican

## Significance tests
# Clustering SEs by respondent


### A lot, Some

# Democrat
df1 <- reppooled.df %>% filter(
  party_id == "Democrat",
  undoc_know %in% c("A lot", "Some")
)

olr1 <- polr(dv_ad7_ord ~ t_7,
  data = df1,
  Hess = TRUE
)

olr_coef1 <- coeftest(olr1,
  vcov = vcovCL(olr1, factor(df1$ResponseId))
)


# Independent
df2 <- reppooled.df %>% filter(
  party_id %in% c(
    "Independent lean Dem",
    "Independent no lean",
    "Independent lean Rep"
  ),
  undoc_know %in% c("A lot", "Some")
)

olr2 <- polr(dv_ad7_ord ~ t_7,
  data = df2,
  Hess = TRUE
)

olr_coef2 <- coeftest(olr2,
  vcov = vcovCL(olr2, factor(df2$ResponseId))
)

# Republican
df3 <- reppooled.df %>% filter(
  party_id == "Republican",
  undoc_know %in% c("A lot", "Some")
)

olr3 <- polr(dv_ad7_ord ~ t_7,
  data = df3,
  Hess = TRUE
)

olr_coef3 <- coeftest(olr3,
  vcov = vcovCL(olr3, factor(df3$ResponseId))
)



### A few

# Democrat
df4 <- reppooled.df %>% filter(
  party_id == "Democrat",
  undoc_know == "A few"
)

olr4 <- polr(dv_ad7_ord ~ t_7,
  data = df4,
  Hess = TRUE
)

olr_coef4 <- coeftest(olr4,
  vcov = vcovCL(olr4, factor(df4$ResponseId))
)


# Independent
df5 <- reppooled.df %>% filter(
  party_id %in% c(
    "Independent lean Dem",
    "Independent no lean",
    "Independent lean Rep"
  ),
  undoc_know == "A few"
)

olr5 <- polr(dv_ad7_ord ~ t_7,
  data = df5,
  Hess = TRUE
)

olr_coef5 <- coeftest(olr5,
  vcov = vcovCL(olr5, factor(df5$ResponseId))
)

# Republican
df6 <- reppooled.df %>% filter(
  party_id == "Republican",
  undoc_know == "A few"
)

olr6 <- polr(dv_ad7_ord ~ t_7,
  data = df6,
  Hess = TRUE
)

olr_coef6 <- coeftest(olr6,
  vcov = vcovCL(olr6, factor(df6$ResponseId))
)



### None

# Democrat
df7 <- reppooled.df %>% filter(
  party_id == "Democrat",
  undoc_know == "None"
)

olr7 <- polr(dv_ad7_ord ~ t_7,
  data = df7,
  Hess = TRUE
)

olr_coef7 <- coeftest(olr7,
  vcov = vcovCL(olr7, factor(df7$ResponseId))
)


# Independent
df8 <- reppooled.df %>% filter(
  party_id %in% c(
    "Independent lean Dem",
    "Independent no lean",
    "Independent lean Rep"
  ),
  undoc_know == "None"
)

olr8 <- polr(dv_ad7_ord ~ t_7,
  data = df8,
  Hess = TRUE
)

olr_coef8 <- coeftest(olr8,
  vcov = vcovCL(olr8, factor(df8$ResponseId))
)

# Republican
df9 <- reppooled.df %>% filter(
  party_id == "Republican",
  undoc_know == "None"
)

olr9 <- polr(dv_ad7_ord ~ t_7,
  data = df9,
  Hess = TRUE
)

olr_coef9 <- coeftest(olr9,
  vcov = vcovCL(olr9, factor(df9$ResponseId))
)





## Job Talk Study 2 Republican Candidate Figure x Gen


## 1st

df1 <- tibble(
  Message = c(
    "Moderate Econ. (R)",
    "Pro-Business Econ. (R)",
    "Pro-Legal (R)",
    "Anti-Undocumented (R)"
  ),
  coefficient = olr_coef1[, 1],
  se = olr_coef1[, 2],
  pid = c(rep("Democrat")),
  undoc_know = c(rep("A lot/Some"))
)

df2 <- tibble(
  Message = c(
    "Moderate Econ. (R)",
    "Pro-Business Econ. (R)",
    "Pro-Legal (R)",
    "Anti-Undocumented (R)"
  ),
  coefficient = olr_coef2[, 1],
  se = olr_coef2[, 2],
  pid = c(rep("Independent")),
  undoc_know = c(rep("A lot/Some"))
)



df3 <- tibble(
  Message = c(
    "Moderate Econ. (R)",
    "Pro-Business Econ. (R)",
    "Pro-Legal (R)",
    "Anti-Undocumented (R)"
  ),
  coefficient = olr_coef5[, 1],
  se = olr_coef3[, 2],
  pid = c(rep("Republican")),
  undoc_know = c(rep("A lot/Some"))
)


## A few


df4 <- tibble(
  Message = c(
    "Moderate Econ. (R)",
    "Pro-Business Econ. (R)",
    "Pro-Legal (R)",
    "Anti-Undocumented (R)"
  ),
  coefficient = olr_coef4[, 1],
  se = olr_coef4[, 2],
  pid = c(rep("Democrat")),
  undoc_know = c(rep("A few"))
)

df5 <- tibble(
  Message = c(
    "Moderate Econ. (R)",
    "Pro-Business Econ. (R)",
    "Pro-Legal (R)",
    "Anti-Undocumented (R)"
  ),
  coefficient = olr_coef5[, 1],
  se = olr_coef5[, 2],
  pid = c(rep("Independent")),
  undoc_know = c(rep("A few"))
)



df6 <- tibble(
  Message = c(
    "Moderate Econ. (R)",
    "Pro-Business Econ. (R)",
    "Pro-Legal (R)",
    "Anti-Undocumented (R)"
  ),
  coefficient = olr_coef6[, 1],
  se = olr_coef6[, 2],
  pid = c(rep("Republican")),
  undoc_know = c(rep("A few"))
)



## None


df7 <- tibble(
  Message = c(
    "Moderate Econ. (R)",
    "Pro-Business Econ. (R)",
    "Pro-Legal (R)",
    "Anti-Undocumented (R)"
  ),
  coefficient = olr_coef7[, 1],
  se = olr_coef7[, 2],
  pid = c(rep("Democrat")),
  undoc_know = c(rep("None"))
)

df8 <- tibble(
  Message = c(
    "Moderate Econ. (R)",
    "Pro-Business Econ. (R)",
    "Pro-Legal (R)",
    "Anti-Undocumented (R)"
  ),
  coefficient = olr_coef8[, 1],
  se = olr_coef8[, 2],
  pid = c(rep("Independent")),
  undoc_know = c(rep("None"))
)



df9 <- tibble(
  Message = c(
    "Moderate Econ. (R)",
    "Pro-Business Econ. (R)",
    "Pro-Legal (R)",
    "Anti-Undocumented (R)"
  ),
  coefficient = olr_coef9[, 1],
  se = olr_coef9[, 2],
  pid = c(rep("Republican")),
  undoc_know = c(rep("None"))
)

repcand.df <- rbind(df1, df2, df3, df4, df5, df6, df7, df8, df9)

## Final dataclean

# Fixing factor levels

repcand.df$pid <- factor(repcand.df$pid,
  levels = c(
    "Democrat",
    "Independent",
    "Republican"
  )
)

repcand.df$Message <- factor(repcand.df$Message,
  levels = c(
    "Anti-Undocumented (R)",
    "Pro-Legal (R)",
    "Pro-Business Econ. (R)",
    "Moderate Econ. (R)"
  )
)

repcand.df$undoc_know <- factor(repcand.df$undoc_know,
  levels = c(
    "A lot/Some",
    "A few",
    "None"
  )
)


## Graph

repcand.plot <- ggplot(
  data = repcand.df,
  aes(
    x = Message,
    y = coefficient
  )
) +
  geom_point(
    aes(
      x = Message,
      y = coefficient,
      color = Message
    ),
    size = 4
  ) +
  geom_linerange(
    aes(
      x = Message,
      color = Message,
      ymin = coefficient - 1.96 * se,
      ymax = coefficient + 1.96 * se
    ),
    lwd = 2
  ) +
  facet_grid(undoc_know ~ pid) +
  coord_flip() +
  labs(
    x = "",
    y = "Estimated Effect of Treatment Assignment on Change in Treatment Ad Outcome (1-5)"
  ) +
  theme(
    legend.position = "none",
    text = element_text(size = 16)
  )

## Adding constant line at 0

repcand.plot + geom_hline(
  data = repcand.df,
  aes(yintercept = 0)
)

ggsave("Final/Appendix/study2_repcandfigure_byundoc_know.png",
  height = 6,
  width = 12
)
```



```{r Study 2 Evaluations -> Turnout}
graph1.df <- study2.cleaned.df %>%
  dplyr::select(
    dv_turnout,
    fisher_adsentiment,
    party_id
  ) %>%
  group_by(
    party_id,
    fisher_adsentiment,
  ) %>%
  summarize(dv = mean(dv_turnout))

graph2.df <- study2.cleaned.df %>%
  dplyr::select(
    dv_turnout,
    miller_adsentiment,
    party_id
  ) %>%
  group_by(
    party_id,
    miller_adsentiment
  ) %>%
  summarize(dv = mean(dv_turnout))

graph1.df$candidate <- rep("Response to Democratic Ads")

graph2.df$candidate <- rep("Response to Republican Ads")

graph1.df <- graph1.df %>% rename(adsentiment = fisher_adsentiment)


graph2.df <- graph2.df %>% rename(adsentiment = miller_adsentiment)


graph.df <- rbind(
  graph1.df,
  graph2.df
)

ggplot(
  data = graph.df,
  aes(
    x = adsentiment,
    y = dv,
    color = party_id
  )
) +
  geom_smooth(method = "loess") +
  facet_grid(party_id ~ candidate) +
  labs(
    y = "Loess Prediction of Hypothetical Turnout (1 = Extremely Unlikely, 5 = Extremely Likely)",
    x = "Sentiment towards All Ads from Democrat (left) or Republican (right)\n(1-5 from each ad, 5-25 scale from 5 ads per candidate)"
  ) +
  guides(col = guide_legend("Respondent Party ID Strength")) +
  theme(
    legend.position = "none",
    axis.title.x = element_text(size = 16),
    axis.title.y = element_text(size = 16),
    axis.text.y = element_text(size = 12),
    axis.text.x = element_text(size = 16),
    strip.text.x = element_text(size = 12),
    strip.text.y = element_text(size = 11)
  ) +
  coord_cartesian(ylim = c(2, 5)) +
  scale_color_manual(values = c(
    "blue2",
    "green4",
    "yellow4",
    "orange2",
    "red4"
  ))

ggsave("Final/Appendix/study2_evaltoturnout_by_partid.png",
  height = 8,
  width = 12
)
```



```{r Study 2 Evaluations -> Vote Choice}
graph1.df <- study2.cleaned.df %>%
  dplyr::select(
    dv_forcechoice,
    fisher_adsentiment,
    party_id
  ) %>%
  group_by(
    party_id,
    fisher_adsentiment,
  ) %>%
  summarize(dv = mean(dv_forcechoice))

graph2.df <- study2.cleaned.df %>%
  dplyr::select(
    dv_forcechoice,
    miller_adsentiment,
    party_id
  ) %>%
  group_by(
    party_id,
    miller_adsentiment
  ) %>%
  summarize(dv = mean(dv_forcechoice))

graph1.df$candidate <- rep("Response to Democratic Ads")

graph2.df$candidate <- rep("Response to Republican Ads")

graph1.df <- graph1.df %>% rename(adsentiment = fisher_adsentiment)


graph2.df <- graph2.df %>% rename(adsentiment = miller_adsentiment)


graph.df <- rbind(
  graph1.df,
  graph2.df
)


## Shortening party_id
graph.df <- graph.df %>%
  mutate(party_id = ifelse(party_id == "Republican",
    "Rep",
    ifelse(party_id == "Independent lean Rep",
      "Ind/Rep",
      ifelse(party_id == "Independent no lean",
        "Ind",
        ifelse(party_id == "Independent lean Dem",
          "Ind/Dem",
          ifelse(party_id == "Democrat",
            "Dem",
            NA
          )
        )
      )
    )
  ))

graph.df$party_id <- factor(graph.df$party_id,
  levels = c(
    "Dem",
    "Ind/Dem",
    "Ind",
    "Ind/Rep",
    "Rep"
  )
)

graph1 <- ggplot(
  data = graph.df,
  aes(
    x = adsentiment,
    y = dv,
    color = party_id
  )
) +
  geom_smooth(method = "loess") +
  facet_grid(party_id ~ candidate) +
  labs(
    y = "Hypothetical Forced Vote (Republican = 0, Democrat = 1)",
    x = "Sentiment towards All Ads from Democrat (left) or Republican (right)\n(1-5 from each ad, 5-25 scale from 5 ads per candidate)"
  ) +
  theme(
    legend.position = "none",
    axis.title.x = element_text(size = 16),
    axis.title.y = element_text(size = 12),
    axis.text.y = element_text(size = 12),
    axis.text.x = element_text(size = 16),
    strip.text.x = element_text(size = 12),
    strip.text.y = element_text(size = 11)
  ) +
  coord_cartesian(ylim = c(0, 1)) +
  scale_color_manual(values = c(
    "blue2",
    "green4",
    "yellow4",
    "orange2",
    "red4"
  ))

graph1 + geom_hline(
  data = graph.df,
  yintercept = 0.5,
  linetype = "dashed",
  lwd = 1
)


ggsave("Final/Appendix/study2_evaltovotechoice_by_partid.png",
  height = 6,
  width = 10
)
```



### Study 3

```{r Study 3: Conjoint functions}
## Conjoint analysis and regression packages

library(sandwich)
library(lmtest)


## Functions

make_attributes <- function(df) {
  df <- within(df, {
    attribute <- factor(attribute,
      levels = c(
        "Latino",
        "Baseline: White",
        "Democrat",
        "Baseline: Republican",
        "Progressive",
        "Services",
        "Infrastructure",
        "Inflation/Small Businesses",
        "Anti-Socialist",
        "Baseline: No Econ. Policy",
        "Pathway for Undocumented",
        "More Legal Immigrants",
        "Border Security",
        "Anti-Undocumented",
        "Base category: No Immig. Policy",
        "Legal Without Restrictions",
        "Legal to 3rd Trimester",
        "Legal to 2nd Trimester",
        "Illegal with Exceptions for Rape/Incest",
        "Baseline: No Abortion Policy"
      )
    )
  })
  return(df)
}

make_coef_group <- function(df) {
  df <- within(df, {
    coef_group <- rep(NA, nrow(df))
    coef_group[grepl(pattern = "Race", x = rowname)] <- "Race"
    coef_group[grepl(pattern = "Party", x = rowname)] <- "Party"
    coef_group[grepl(pattern = "Economics", x = rowname)] <- "Economics"
    coef_group[grepl(pattern = "Immigration", x = rowname)] <- "Immigration"
    coef_group[grepl(pattern = "Abortion", x = rowname)] <- "Abortion"
    coef_group <- factor(coef_group, levels = c(
      "Party",
      "Race",
      "Economics",
      "Immigration",
      "Abortion"
    ))
  })
  return(df)
}

cl <- function(dat, fm, cluster) {
  require(sandwich, quietly = TRUE)
  require(lmtest, quietly = TRUE)
  M <- length(unique(cluster))
  N <- length(cluster)
  K <- fm$rank
  dfc <- (M / (M - 1)) * ((N - 1) / (N - K))
  uj <- apply(estfun(fm), 2, function(x) tapply(x, cluster, sum))
  vcovCL <- dfc * sandwich(fm, meat = crossprod(uj) / N)
  coeftest(fm, vcovCL)
}
```




```{r Study 3: Main figure prep}


## Fixing factor levels

conjoint.df$Race <- factor(conjoint.df$Race,
                           levels = c("White",
                                      "Latino"))

conjoint.df$Party <- factor(conjoint.df$Party,
                            levels = c("Republican",
                                       "Democrat"))

conjoint.df$Economics <- factor(conjoint.df$Economics,
                            levels = c("No Policy",
                                       "Progressive",
                                       "Services",
                                       "Infrastructure",
                                       "Inflation/Small Businesses",
                                       "Anti-Socialism"))

conjoint.df$Immigration <- factor(conjoint.df$Immigration,
                            levels = c("No Policy",
                                       "Pathway for Undocumented",
                                       "More Legal Immigrants",
                                       "Border Security",
                                       "Anti-Undocumented"))

conjoint.df$Abortion <- factor(conjoint.df$Abortion,
                            levels = c("No Policy",
                                       "Legal Without Restrictions",
                                       "Legal to 3rd Trimester",
                                       "Legal to 2nd Trimester",
                                       "Illegal With Exceptions for Rape/Incest"))



## Heterogeneous effects by PID

dems.df <- conjoint.df %>%
  filter(Party.ID == "Democrat")

indleandems.df <- conjoint.df %>%
  filter(Party.ID == "Independent lean Dem")

indnoleans.df <- conjoint.df %>%
  filter(Party.ID == "Independent no lean")

indleanreps.df <- conjoint.df %>%
  filter(Party.ID == "Independent lean Rep")

reps.df <- conjoint.df %>%
  filter(Party.ID == "Republican")



## Democrats first

## Run LM for AMCEs

dem.lm <- lm(
  data = dems.df,
  Vote ~ Party + Race + Economics + Immigration + Abortion
)

## Use cluster function to calculate respondent-clustered SEs

dem.lm.cl <- cl(
  dat = dems.df,
  cluster = dems.df$ResponseId,
  fm = dem.lm
)

dem.lm.df <- data.frame(dem.lm.cl[, ]) %>%
  rownames_to_column() %>%
  filter(rowname != "(Intercept)")

dem.lm.df <- dem.lm.df %>%
  mutate(
    lis = Estimate - 1.96 * Std..Error,
    ulis = Estimate + 1.96 * Std..Error
  )

dem.lm.df <- dem.lm.df %>%
  dplyr::select(rowname,
    Estimate,
    signif = Pr...t..,
    lis,
    ulis
  )

## Add baseline categories back

baselines.df <- data.frame(
  rowname = c(
    "Baseline: Republican",
    "Baseline: White",
    "Baseline: No Econ. Policy",
    "Baseline: No Immig. Policy",
    "Baseline: No Abortion Policy"
  ),
  Estimate = c(0, 0, 0, 0, 0),
  lis = c(0, 0, 0, 0, 0),
  ulis = c(0, 0, 0, 0, 0),
  signif = c(rep(1)),
  attribute = c(
    "Party",
    "Race",
    "Economics",
    "Immigration",
    "Abortion"
  )
)

## Add attribute for facets

dem.lm.df$attribute <- c(
  "Party",
  "Race",
  "Economics", "Economics", "Economics", "Economics", "Economics",
  "Immigration", "Immigration", "Immigration", "Immigration",
  "Abortion", "Abortion", "Abortion", "Abortion"
)

plot1.df <- rbind(
  dem.lm.df,
  baselines.df
)

## Add party ID variable

plot1.df$`Party ID` <- c(rep("Democrat"))





## Independent lean Dems

## Run LM for AMCEs

indleandem.lm <- lm(
  data = indleandems.df,
  Vote ~ Party + Race + Economics + Immigration + Abortion
)

## Use cluster function to calculate respondent-clustered SEs

indleandem.lm.cl <- cl(
  dat = indleandems.df,
  cluster = indleandems.df$ResponseId,
  fm = indleandem.lm
)

indleandem.lm.df <- data.frame(indleandem.lm.cl[, ]) %>%
  rownames_to_column() %>%
  filter(rowname != "(Intercept)")

indleandem.lm.df <- indleandem.lm.df %>%
  mutate(
    lis = Estimate - 1.96 * Std..Error,
    ulis = Estimate + 1.96 * Std..Error
  )

indleandem.lm.df <- indleandem.lm.df %>%
  dplyr::select(rowname,
    Estimate,
    signif = Pr...t..,
    lis,
    ulis
  )

## Add attribute for facets

indleandem.lm.df$attribute <- c(
  "Party",
  "Race",
  "Economics", "Economics", "Economics", "Economics", "Economics",
  "Immigration", "Immigration", "Immigration", "Immigration",
  "Abortion", "Abortion", "Abortion", "Abortion"
)

plot2.df <- rbind(
  indleandem.lm.df,
  baselines.df
)

## Add party ID variable

plot2.df$`Party ID` <- c(rep("Ind-Lean Dem"))



## Independent no lean

## Run LM for AMCEs

indnolean.lm <- lm(
  data = indnoleans.df,
  Vote ~ Party + Race + Economics + Immigration + Abortion
)

## Use cluster function to calculate respondent-clustered SEs

indnolean.lm.cl <- cl(
  dat = indnoleans.df,
  cluster = indnoleans.df$ResponseId,
  fm = indnolean.lm
)

indnolean.lm.df <- data.frame(indnolean.lm.cl[, ]) %>%
  rownames_to_column() %>%
  filter(rowname != "(Intercept)")

indnolean.lm.df <- indnolean.lm.df %>%
  mutate(
    lis = Estimate - 1.96 * Std..Error,
    ulis = Estimate + 1.96 * Std..Error
  )

indnolean.lm.df <- indnolean.lm.df %>%
  dplyr::select(rowname,
    Estimate,
    signif = Pr...t..,
    lis,
    ulis
  )


## Add attribute for facets

indnolean.lm.df$attribute <- c(
  "Party",
  "Race",
  "Economics", "Economics", "Economics", "Economics", "Economics",
  "Immigration", "Immigration", "Immigration", "Immigration",
  "Abortion", "Abortion", "Abortion", "Abortion"
)

plot3.df <- rbind(
  indnolean.lm.df,
  baselines.df
)

## Add party ID variable

plot3.df$`Party ID` <- c(rep("Ind-No lean"))





## Independent lean Reps

## Run LM for AMCEs

indleanrep.lm <- lm(
  data = indleanreps.df,
  Vote ~ Party + Race + Economics + Immigration + Abortion
)

## Use cluster function to calculate respondent-clustered SEs

indleanrep.lm.cl <- cl(
  dat = indleanreps.df,
  cluster = indleanreps.df$ResponseId,
  fm = indleanrep.lm
)

indleanrep.lm.df <- data.frame(indleanrep.lm.cl[, ]) %>%
  rownames_to_column() %>%
  filter(rowname != "(Intercept)")

indleanrep.lm.df <- indleanrep.lm.df %>%
  mutate(
    lis = Estimate - 1.96 * Std..Error,
    ulis = Estimate + 1.96 * Std..Error
  )

indleanrep.lm.df <- indleanrep.lm.df %>%
  dplyr::select(rowname,
    Estimate,
    signif = Pr...t..,
    lis,
    ulis
  )

## Add attribute for facets

indleanrep.lm.df$attribute <- c(
  "Party",
  "Race",
  "Economics", "Economics", "Economics", "Economics", "Economics",
  "Immigration", "Immigration", "Immigration", "Immigration",
  "Abortion", "Abortion", "Abortion", "Abortion"
)

plot4.df <- rbind(
  indleanrep.lm.df,
  baselines.df
)

## Add party ID variable

plot4.df$`Party ID` <- c(rep("Ind-Lean Rep"))




## Republicans

## Run LM for AMCEs

rep.lm <- lm(
  data = reps.df,
  Vote ~ Party + Race + Economics + Immigration + Abortion
)

## Use cluster function to calculate respondent-clustered SEs

rep.lm.cl <- cl(
  dat = reps.df,
  cluster = reps.df$ResponseId,
  fm = rep.lm
)

rep.lm.df <- data.frame(rep.lm.cl[, ]) %>%
  rownames_to_column() %>%
  filter(rowname != "(Intercept)")

rep.lm.df <- rep.lm.df %>%
  mutate(
    lis = Estimate - 1.96 * Std..Error,
    ulis = Estimate + 1.96 * Std..Error
  )

rep.lm.df <- rep.lm.df %>%
  dplyr::select(rowname,
    Estimate,
    signif = Pr...t..,
    lis,
    ulis
  )


## Add attribute for facets

rep.lm.df$attribute <- c(
  "Party",
  "Race",
  "Economics", "Economics", "Economics", "Economics", "Economics",
  "Immigration", "Immigration", "Immigration", "Immigration",
  "Abortion", "Abortion", "Abortion", "Abortion"
)

plot5.df <- rbind(
  rep.lm.df,
  baselines.df
)

## Add party ID variable

plot5.df$`Party ID` <- c(rep("Republican"))




### All five combined

## First combine with baselines

pid.5pt.df <- rbind(
  plot1.df,
  plot2.df,
  plot3.df,
  plot4.df,
  plot5.df
)

pid.5pt.df$PID <- factor(pid.5pt.df$`Party ID`,
  levels = c(
    "Democrat",
    "Independent lean Dem.",
    "Independent no lean",
    "Independent lean Rep.",
    "Republican"
  )
)


## Fix factor levels

pid.5pt.df$attribute <- factor(pid.5pt.df$attribute,
  levels = c(
    "Party",
    "Race",
    "Economics",
    "Immigration",
    "Abortion"
  )
)

## Fix row names and factor level

pid.5pt.df <- pid.5pt.df %>%
  mutate(`Candidate Attribute/Policy` = ifelse(rowname == "PartyDemocrat",
    "Democrat",
    ifelse(rowname == "PartyRepublican",
      "Baseline: Republican",
      ifelse(rowname == "RaceLatino",
        "Latino",
        ifelse(rowname == "RaceWhite",
          "Baseline: White",
          ifelse(rowname == "EconomicsNo Policy",
            "Baseline: No Econ. Policy",
            ifelse(rowname == "EconomicsProgressive",
              "Progressive",
              ifelse(rowname == "EconomicsServices",
                "Services",
                ifelse(rowname == "EconomicsInfrastructure",
                  "Infrastructure",
                  ifelse(rowname == "EconomicsInflation/Small Businesses",
                    "Inflation/Small Businesses",
                    ifelse(rowname == "EconomicsAnti-Socialism",
                      "Anti-Socialism",
                      ifelse(rowname == "ImmigrationNo Policy",
                        "Baseline: No Immig. Policy",
                        ifelse(rowname == "ImmigrationPathway for Undocumented",
                          "Pathway for Undocumented",
                          ifelse(rowname == "ImmigrationMore Legal Immigrants",
                            "More Legal Immigrants",
                            ifelse(rowname == "ImmigrationBorder Security",
                              "Border Security",
                              ifelse(rowname == "ImmigrationAnti-Undocumented",
                                "Anti-Undocumented",
                                ifelse(rowname == "AbortionNo Policy",
                                  "Baseline: No Abortion Policy",
                                  ifelse(rowname == "AbortionLegal Without Restrictions",
                                    "Legal Without Restrictions",
                                    ifelse(rowname == "AbortionLegal to 3rd Trimester",
                                      "Legal to 3rd Trimester",
                                      ifelse(rowname == "AbortionLegal to 2nd Trimester",
                                        "Legal to 2nd Trimester",
                                        ifelse(rowname == "AbortionIllegal With Exceptions for Rape/Incest",
                                          "Illegal with Exceptions for Rape/Incest",
                                          rowname
                                        )
                                      )
                                    )
                                  )
                                )
                              )
                            )
                          )
                        )
                      )
                    )
                  )
                )
              )
            )
          )
        )
      )
    )
  ))


pid.5pt.df$`Candidate Attribute/Policy` <- factor(pid.5pt.df$`Candidate Attribute/Policy`,
  levels = c(
    "Illegal with Exceptions for Rape/Incest",
    "Legal to 2nd Trimester",
    "Legal to 3rd Trimester",
    "Legal Without Restrictions",
    "Baseline: No Abortion Policy",
    "Anti-Undocumented",
    "Border Security",
    "More Legal Immigrants",
    "Pathway for Undocumented",
    "Baseline: No Immig. Policy",
    "Anti-Socialism",
    "Inflation/Small Businesses",
    "Infrastructure",
    "Services",
    "Progressive",
    "Baseline: No Econ. Policy",
    "Latino",
    "Baseline: White",
    "Democrat",
    "Baseline: Republican"
  )
)
```



```{r Study 3: Main Plot}
### Plot, point size = significances

temp.df <- pid.5pt.df %>%
  mutate(signif_size = ifelse(signif < 0.05,
    3,
    1
  ))

ggplot(
  temp.df,
  aes(
    x = Estimate,
    y = `Candidate Attribute/Policy`,
    color = `Party ID`
  )
) +
  geom_point(size = temp.df$signif_size) +
  geom_segment(aes(
    yend = `Candidate Attribute/Policy`,
    x = lis,
    xend = ulis
  )) +
  geom_vline(xintercept = 0, linetype = "dashed") +
  coord_cartesian(c(-0.5, 0.65)) +
  facet_grid(attribute ~ `Party ID`, scales = "free_y") +
  scale_color_manual(values = c(
    "blue2",
    "green4",
    "yellow4",
    "orange2",
    "red4"
  )) +
  labs(x = "Estimated Change in Vote Choice (0 or 1)")+
  theme(legend.position = "none")

ggsave("Final/study3_mainfigure.jpg",
  height = 8,
  width = 12
)
```




```{r Study 3 Appendix: Leaners as partisans}


## Fixing factor levels

conjoint.df$Race <- factor(conjoint.df$Race,
                           levels = c("White",
                                      "Latino"))

conjoint.df$Party <- factor(conjoint.df$Party,
                            levels = c("Republican",
                                       "Democrat"))

conjoint.df$Economics <- factor(conjoint.df$Economics,
                            levels = c("No Policy",
                                       "Progressive",
                                       "Services",
                                       "Infrastructure",
                                       "Inflation/Small Businesses",
                                       "Anti-Socialism"))

conjoint.df$Immigration <- factor(conjoint.df$Immigration,
                            levels = c("No Policy",
                                       "Pathway for Undocumented",
                                       "More Legal Immigrants",
                                       "Border Security",
                                       "Anti-Undocumented"))

conjoint.df$Abortion <- factor(conjoint.df$Abortion,
                            levels = c("No Policy",
                                       "Legal Without Restrictions",
                                       "Legal to 3rd Trimester",
                                       "Legal to 2nd Trimester",
                                       "Illegal With Exceptions for Rape/Incest"))



## Heterogeneous effects by PID

dems.df <- conjoint.df %>%
  filter(Party.ID %in% c("Democrat",
                         "Independent lean Dem"))

indnoleans.df <- conjoint.df %>%
  filter(Party.ID == "Independent no lean")

reps.df <- conjoint.df %>%
  filter(Party.ID %in% c("Independent lean Rep",
                         "Republican"))



## Democrats first

## Run LM for AMCEs

dem.lm <- lm(
  data = dems.df,
  Vote ~ Party + Race + Economics + Immigration + Abortion
)

## Use cluster function to calculate respondent-clustered SEs

dem.lm.cl <- cl(
  dat = dems.df,
  cluster = dems.df$ResponseId,
  fm = dem.lm
)

dem.lm.df <- data.frame(dem.lm.cl[, ]) %>%
  rownames_to_column() %>%
  filter(rowname != "(Intercept)")

dem.lm.df <- dem.lm.df %>%
  mutate(
    lis = Estimate - 1.96 * Std..Error,
    ulis = Estimate + 1.96 * Std..Error
  )

dem.lm.df <- dem.lm.df %>%
  dplyr::select(rowname,
    Estimate,
    signif = Pr...t..,
    lis,
    ulis
  )

## Add baseline categories back

baselines.df <- data.frame(
  rowname = c(
    "Baseline: Republican",
    "Baseline: White",
    "Baseline: No Econ. Policy",
    "Baseline: No Immig. Policy",
    "Baseline: No Abortion Policy"
  ),
  Estimate = c(0, 0, 0, 0, 0),
  lis = c(0, 0, 0, 0, 0),
  ulis = c(0, 0, 0, 0, 0),
  signif = c(rep(1)),
  attribute = c(
    "Party",
    "Race",
    "Economics",
    "Immigration",
    "Abortion"
  )
)

## Add attribute for facets

dem.lm.df$attribute <- c(
  "Party",
  "Race",
  "Economics", "Economics", "Economics", "Economics", "Economics",
  "Immigration", "Immigration", "Immigration", "Immigration",
  "Abortion", "Abortion", "Abortion", "Abortion"
)

plot1.df <- rbind(
  dem.lm.df,
  baselines.df
)

## Add party ID variable

plot1.df$`Party ID` <- c(rep("Dem/Ind lean Dem"))



## Independent no lean

## Run LM for AMCEs

indnolean.lm <- lm(
  data = indnoleans.df,
  Vote ~ Party + Race + Economics + Immigration + Abortion
)

## Use cluster function to calculate respondent-clustered SEs

indnolean.lm.cl <- cl(
  dat = indnoleans.df,
  cluster = indnoleans.df$ResponseId,
  fm = indnolean.lm
)

indnolean.lm.df <- data.frame(indnolean.lm.cl[, ]) %>%
  rownames_to_column() %>%
  filter(rowname != "(Intercept)")

indnolean.lm.df <- indnolean.lm.df %>%
  mutate(
    lis = Estimate - 1.96 * Std..Error,
    ulis = Estimate + 1.96 * Std..Error
  )

indnolean.lm.df <- indnolean.lm.df %>%
  dplyr::select(rowname,
    Estimate,
    signif = Pr...t..,
    lis,
    ulis
  )


## Add attribute for facets

indnolean.lm.df$attribute <- c(
  "Party",
  "Race",
  "Economics", "Economics", "Economics", "Economics", "Economics",
  "Immigration", "Immigration", "Immigration", "Immigration",
  "Abortion", "Abortion", "Abortion", "Abortion"
)

plot3.df <- rbind(
  indnolean.lm.df,
  baselines.df
)

## Add party ID variable

plot3.df$`Party ID` <- c(rep("Ind - No lean"))


## Republicans

## Run LM for AMCEs

rep.lm <- lm(
  data = reps.df,
  Vote ~ Party + Race + Economics + Immigration + Abortion
)

## Use cluster function to calculate respondent-clustered SEs

rep.lm.cl <- cl(
  dat = reps.df,
  cluster = reps.df$ResponseId,
  fm = rep.lm
)

rep.lm.df <- data.frame(rep.lm.cl[, ]) %>%
  rownames_to_column() %>%
  filter(rowname != "(Intercept)")

rep.lm.df <- rep.lm.df %>%
  mutate(
    lis = Estimate - 1.96 * Std..Error,
    ulis = Estimate + 1.96 * Std..Error
  )

rep.lm.df <- rep.lm.df %>%
  dplyr::select(rowname,
    Estimate,
    signif = Pr...t..,
    lis,
    ulis
  )


## Add attribute for facets

rep.lm.df$attribute <- c(
  "Party",
  "Race",
  "Economics", "Economics", "Economics", "Economics", "Economics",
  "Immigration", "Immigration", "Immigration", "Immigration",
  "Abortion", "Abortion", "Abortion", "Abortion"
)

plot5.df <- rbind(
  rep.lm.df,
  baselines.df
)

## Add party ID variable

plot5.df$`Party ID` <- c(rep("Rep/Ind lean Rep"))




### All five combined

## First combine with baselines

pid.5pt.df <- rbind(
  plot1.df,
  plot3.df,
  plot5.df
)

pid.5pt.df$PID <- factor(pid.5pt.df$`Party ID`,
  levels = c(
    "Dem/Ind lean Dem",
    "Ind - No lean",
    "Rep/Ind lean Rep"
  )
)


## Fix factor levels

pid.5pt.df$attribute <- factor(pid.5pt.df$attribute,
  levels = c(
    "Party",
    "Race",
    "Economics",
    "Immigration",
    "Abortion"
  )
)

## Fix row names and factor level

pid.5pt.df <- pid.5pt.df %>%
  mutate(`Candidate Attribute/Policy` = ifelse(rowname == "PartyDemocrat",
    "Democrat",
    ifelse(rowname == "PartyRepublican",
      "Baseline: Republican",
      ifelse(rowname == "RaceLatino",
        "Latino",
        ifelse(rowname == "RaceWhite",
          "Baseline: White",
          ifelse(rowname == "EconomicsNo Policy",
            "Baseline: No Econ. Policy",
            ifelse(rowname == "EconomicsProgressive",
              "Progressive",
              ifelse(rowname == "EconomicsServices",
                "Services",
                ifelse(rowname == "EconomicsInfrastructure",
                  "Infrastructure",
                  ifelse(rowname == "EconomicsInflation/Small Businesses",
                    "Inflation/Small Businesses",
                    ifelse(rowname == "EconomicsAnti-Socialism",
                      "Anti-Socialism",
                      ifelse(rowname == "ImmigrationNo Policy",
                        "Baseline: No Immig. Policy",
                        ifelse(rowname == "ImmigrationPathway for Undocumented",
                          "Pathway for Undocumented",
                          ifelse(rowname == "ImmigrationMore Legal Immigrants",
                            "More Legal Immigrants",
                            ifelse(rowname == "ImmigrationBorder Security",
                              "Border Security",
                              ifelse(rowname == "ImmigrationAnti-Undocumented",
                                "Anti-Undocumented",
                                ifelse(rowname == "AbortionNo Policy",
                                  "Baseline: No Abortion Policy",
                                  ifelse(rowname == "AbortionLegal Without Restrictions",
                                    "Legal Without Restrictions",
                                    ifelse(rowname == "AbortionLegal to 3rd Trimester",
                                      "Legal to 3rd Trimester",
                                      ifelse(rowname == "AbortionLegal to 2nd Trimester",
                                        "Legal to 2nd Trimester",
                                        ifelse(rowname == "AbortionIllegal With Exceptions for Rape/Incest",
                                          "Illegal with Exceptions for Rape/Incest",
                                          rowname
                                        )
                                      )
                                    )
                                  )
                                )
                              )
                            )
                          )
                        )
                      )
                    )
                  )
                )
              )
            )
          )
        )
      )
    )
  ))


pid.5pt.df$`Candidate Attribute/Policy` <- factor(pid.5pt.df$`Candidate Attribute/Policy`,
  levels = c(
    "Illegal with Exceptions for Rape/Incest",
    "Legal to 2nd Trimester",
    "Legal to 3rd Trimester",
    "Legal Without Restrictions",
    "Baseline: No Abortion Policy",
    "Anti-Undocumented",
    "Border Security",
    "More Legal Immigrants",
    "Pathway for Undocumented",
    "Baseline: No Immig. Policy",
    "Anti-Socialism",
    "Inflation/Small Businesses",
    "Infrastructure",
    "Services",
    "Progressive",
    "Baseline: No Econ. Policy",
    "Latino",
    "Baseline: White",
    "Democrat",
    "Baseline: Republican"
  )
)


### Plot, point size = significances

temp.df <- pid.5pt.df %>%
  mutate(signif_size = ifelse(signif < 0.05,
    3,
    1
  ))

ggplot(
  temp.df,
  aes(
    x = Estimate,
    y = `Candidate Attribute/Policy`,
    color = PID
  )
) +
  geom_point(size = temp.df$signif_size) +
  geom_segment(aes(
    yend = `Candidate Attribute/Policy`,
    x = lis,
    xend = ulis
  )) +
  geom_vline(xintercept = 0, linetype = "dashed") +
  coord_cartesian(c(-0.5, 0.65)) +
  facet_grid(attribute ~ PID, scales = "free_y") +
  scale_color_manual(values = c(
    "blue2",
    "yellow4",
    "red4"
  )) +
  labs(x = "Estimated Change in Vote Choice (0 or 1)")+
  theme(legend.position = "none")

ggsave("Final/Appendix/study3_leanersaspartisans.jpg",
  height = 8,
  width = 12
)
```


```{r Study 3 Appendix: Linked fate as predictor}


## Fixing factor levels

conjoint.df$Race <- factor(conjoint.df$Race,
                           levels = c("White",
                                      "Latino"))

conjoint.df$Party <- factor(conjoint.df$Party,
                            levels = c("Republican",
                                       "Democrat"))

conjoint.df$Economics <- factor(conjoint.df$Economics,
                            levels = c("No Policy",
                                       "Progressive",
                                       "Services",
                                       "Infrastructure",
                                       "Inflation/Small Businesses",
                                       "Anti-Socialism"))

conjoint.df$Immigration <- factor(conjoint.df$Immigration,
                            levels = c("No Policy",
                                       "Pathway for Undocumented",
                                       "More Legal Immigrants",
                                       "Border Security",
                                       "Anti-Undocumented"))

conjoint.df$Abortion <- factor(conjoint.df$Abortion,
                            levels = c("No Policy",
                                       "Legal Without Restrictions",
                                       "Legal to 3rd Trimester",
                                       "Legal to 2nd Trimester",
                                       "Illegal With Exceptions for Rape/Incest"))



## Heterogeneous effects by Linked Fate

diag_df <- conjoint.df %>%
  group_by(Linked.Fate) %>%
  reframe(count=n())


lf1.df <- conjoint.df %>%
  filter(Linked.Fate == "None at all")

lf2.df <- conjoint.df %>%
  filter(Linked.Fate == "A little")

lf3.df <- conjoint.df %>%
  filter(Linked.Fate == "A moderate amount")

lf4.df <- conjoint.df %>%
  filter(Linked.Fate == "A lot")

lf5.df <- conjoint.df %>%
  filter(Linked.Fate == "A great deal")


## None (lf1)

## Run LM for AMCEs

dem.lm <- lm(
  data = lf1.df,
  Vote ~ Party + Race + Economics + Immigration + Abortion
)

## Use cluster function to calculate respondent-clustered SEs

dem.lm.cl <- cl(
  dat = lf1.df,
  cluster = lf1.df$ResponseId,
  fm = dem.lm
)

dem.lm.df <- data.frame(dem.lm.cl[, ]) %>%
  rownames_to_column() %>%
  filter(rowname != "(Intercept)")

dem.lm.df <- dem.lm.df %>%
  mutate(
    lis = Estimate - 1.96 * Std..Error,
    ulis = Estimate + 1.96 * Std..Error
  )

dem.lm.df <- dem.lm.df %>%
  dplyr::select(rowname,
    Estimate,
    signif = Pr...t..,
    lis,
    ulis
  )

## Add baseline categories back

baselines.df <- data.frame(
  rowname = c(
    "Baseline: Republican",
    "Baseline: White",
    "Baseline: No Econ. Policy",
    "Baseline: No Immig. Policy",
    "Baseline: No Abortion Policy"
  ),
  Estimate = c(0, 0, 0, 0, 0),
  lis = c(0, 0, 0, 0, 0),
  ulis = c(0, 0, 0, 0, 0),
  signif = c(rep(1)),
  attribute = c(
    "Party",
    "Race",
    "Economics",
    "Immigration",
    "Abortion"
  )
)

## Add attribute for facets

dem.lm.df$attribute <- c(
  "Party",
  "Race",
  "Economics", "Economics", "Economics", "Economics", "Economics",
  "Immigration", "Immigration", "Immigration", "Immigration",
  "Abortion", "Abortion", "Abortion", "Abortion"
)

plot1.df <- rbind(
  dem.lm.df,
  baselines.df
)

## Add party ID variable

plot1.df$`Linked Fate` <- c(rep("None at all"))


## A little

## Run LM for AMCEs

dem.lm <- lm(
  data = lf2.df,
  Vote ~ Party + Race + Economics + Immigration + Abortion
)

## Use cluster function to calculate respondent-clustered SEs

dem.lm.cl <- cl(
  dat = lf2.df,
  cluster = lf2.df$ResponseId,
  fm = dem.lm
)

dem.lm.df <- data.frame(dem.lm.cl[, ]) %>%
  rownames_to_column() %>%
  filter(rowname != "(Intercept)")

dem.lm.df <- dem.lm.df %>%
  mutate(
    lis = Estimate - 1.96 * Std..Error,
    ulis = Estimate + 1.96 * Std..Error
  )

dem.lm.df <- dem.lm.df %>%
  dplyr::select(rowname,
    Estimate,
    signif = Pr...t..,
    lis,
    ulis
  )

## Add baseline categories back

baselines.df <- data.frame(
  rowname = c(
    "Baseline: Republican",
    "Baseline: White",
    "Baseline: No Econ. Policy",
    "Baseline: No Immig. Policy",
    "Baseline: No Abortion Policy"
  ),
  Estimate = c(0, 0, 0, 0, 0),
  lis = c(0, 0, 0, 0, 0),
  ulis = c(0, 0, 0, 0, 0),
  signif = c(rep(1)),
  attribute = c(
    "Party",
    "Race",
    "Economics",
    "Immigration",
    "Abortion"
  )
)

## Add attribute for facets

dem.lm.df$attribute <- c(
  "Party",
  "Race",
  "Economics", "Economics", "Economics", "Economics", "Economics",
  "Immigration", "Immigration", "Immigration", "Immigration",
  "Abortion", "Abortion", "Abortion", "Abortion"
)

plot2.df <- rbind(
  dem.lm.df,
  baselines.df
)

## Add party ID variable

plot2.df$`Linked Fate` <- c(rep("A little"))


## Moderate

## Run LM for AMCEs

dem.lm <- lm(
  data = lf3.df,
  Vote ~ Party + Race + Economics + Immigration + Abortion
)

## Use cluster function to calculate respondent-clustered SEs

dem.lm.cl <- cl(
  dat = lf3.df,
  cluster = lf3.df$ResponseId,
  fm = dem.lm
)

dem.lm.df <- data.frame(dem.lm.cl[, ]) %>%
  rownames_to_column() %>%
  filter(rowname != "(Intercept)")

dem.lm.df <- dem.lm.df %>%
  mutate(
    lis = Estimate - 1.96 * Std..Error,
    ulis = Estimate + 1.96 * Std..Error
  )

dem.lm.df <- dem.lm.df %>%
  dplyr::select(rowname,
    Estimate,
    signif = Pr...t..,
    lis,
    ulis
  )

## Add baseline categories back

baselines.df <- data.frame(
  rowname = c(
    "Baseline: Republican",
    "Baseline: White",
    "Baseline: No Econ. Policy",
    "Baseline: No Immig. Policy",
    "Baseline: No Abortion Policy"
  ),
  Estimate = c(0, 0, 0, 0, 0),
  lis = c(0, 0, 0, 0, 0),
  ulis = c(0, 0, 0, 0, 0),
  signif = c(rep(1)),
  attribute = c(
    "Party",
    "Race",
    "Economics",
    "Immigration",
    "Abortion"
  )
)

## Add attribute for facets

dem.lm.df$attribute <- c(
  "Party",
  "Race",
  "Economics", "Economics", "Economics", "Economics", "Economics",
  "Immigration", "Immigration", "Immigration", "Immigration",
  "Abortion", "Abortion", "Abortion", "Abortion"
)

plot3.df <- rbind(
  dem.lm.df,
  baselines.df
)

## Add party ID variable

plot3.df$`Linked Fate` <- c(rep("Moderate"))


## A lot

## Run LM for AMCEs

dem.lm <- lm(
  data = lf4.df,
  Vote ~ Party + Race + Economics + Immigration + Abortion
)

## Use cluster function to calculate respondent-clustered SEs

dem.lm.cl <- cl(
  dat = lf4.df,
  cluster = lf4.df$ResponseId,
  fm = dem.lm
)

dem.lm.df <- data.frame(dem.lm.cl[, ]) %>%
  rownames_to_column() %>%
  filter(rowname != "(Intercept)")

dem.lm.df <- dem.lm.df %>%
  mutate(
    lis = Estimate - 1.96 * Std..Error,
    ulis = Estimate + 1.96 * Std..Error
  )

dem.lm.df <- dem.lm.df %>%
  dplyr::select(rowname,
    Estimate,
    signif = Pr...t..,
    lis,
    ulis
  )

## Add baseline categories back

baselines.df <- data.frame(
  rowname = c(
    "Baseline: Republican",
    "Baseline: White",
    "Baseline: No Econ. Policy",
    "Baseline: No Immig. Policy",
    "Baseline: No Abortion Policy"
  ),
  Estimate = c(0, 0, 0, 0, 0),
  lis = c(0, 0, 0, 0, 0),
  ulis = c(0, 0, 0, 0, 0),
  signif = c(rep(1)),
  attribute = c(
    "Party",
    "Race",
    "Economics",
    "Immigration",
    "Abortion"
  )
)


## Add attribute for facets

dem.lm.df$attribute <- c(
  "Party",
  "Race",
  "Economics", "Economics", "Economics", "Economics", "Economics",
  "Immigration", "Immigration", "Immigration", "Immigration",
  "Abortion", "Abortion", "Abortion", "Abortion"
)

plot4.df <- rbind(
  dem.lm.df,
  baselines.df
)

## Add party ID variable

plot4.df$`Linked Fate` <- c(rep("A lot"))



## A great deal

## Run LM for AMCEs

dem.lm <- lm(
  data = lf5.df,
  Vote ~ Party + Race + Economics + Immigration + Abortion
)

## Use cluster function to calculate respondent-clustered SEs

dem.lm.cl <- cl(
  dat = lf5.df,
  cluster = lf5.df$ResponseId,
  fm = dem.lm
)

dem.lm.df <- data.frame(dem.lm.cl[, ]) %>%
  rownames_to_column() %>%
  filter(rowname != "(Intercept)")

dem.lm.df <- dem.lm.df %>%
  mutate(
    lis = Estimate - 1.96 * Std..Error,
    ulis = Estimate + 1.96 * Std..Error
  )

dem.lm.df <- dem.lm.df %>%
  dplyr::select(rowname,
    Estimate,
    signif = Pr...t..,
    lis,
    ulis
  )

## Add baseline categories back

baselines.df <- data.frame(
  rowname = c(
    "Baseline: Republican",
    "Baseline: White",
    "Baseline: No Econ. Policy",
    "Baseline: No Immig. Policy",
    "Baseline: No Abortion Policy"
  ),
  Estimate = c(0, 0, 0, 0, 0),
  lis = c(0, 0, 0, 0, 0),
  ulis = c(0, 0, 0, 0, 0),
  signif = c(rep(1)),
  attribute = c(
    "Party",
    "Race",
    "Economics",
    "Immigration",
    "Abortion"
  )
)

## Add attribute for facets

dem.lm.df$attribute <- c(
  "Party",
  "Race",
  "Economics", "Economics", "Economics", "Economics", "Economics",
  "Immigration", "Immigration", "Immigration", "Immigration",
  "Abortion", "Abortion", "Abortion", "Abortion"
)

plot5.df <- rbind(
  dem.lm.df,
  baselines.df
)

## Add party ID variable

plot5.df$`Linked Fate` <- c(rep("A great deal"))




### All five combined

## First combine with baselines

pid.5pt.df <- rbind(
  plot1.df,
  plot2.df,
  plot3.df,
  plot4.df,
  plot5.df
)

pid.5pt.df$`Linked Fate` <- factor(pid.5pt.df$`Linked Fate`,
  levels = c(
    "None at all",
    "A little",
    "Moderate",
    "A lot",
    "A great deal"
  )
)


## Fix factor levels

pid.5pt.df$attribute <- factor(pid.5pt.df$attribute,
  levels = c(
    "Party",
    "Race",
    "Economics",
    "Immigration",
    "Abortion"
  )
)

## Fix row names and factor level

pid.5pt.df <- pid.5pt.df %>%
  mutate(`Candidate Attribute/Policy` = ifelse(rowname == "PartyDemocrat",
    "Democrat",
    ifelse(rowname == "PartyRepublican",
      "Baseline: Republican",
      ifelse(rowname == "RaceLatino",
        "Latino",
        ifelse(rowname == "RaceWhite",
          "Baseline: White",
          ifelse(rowname == "EconomicsNo Policy",
            "Baseline: No Econ. Policy",
            ifelse(rowname == "EconomicsProgressive",
              "Progressive",
              ifelse(rowname == "EconomicsServices",
                "Services",
                ifelse(rowname == "EconomicsInfrastructure",
                  "Infrastructure",
                  ifelse(rowname == "EconomicsInflation/Small Businesses",
                    "Inflation/Small Businesses",
                    ifelse(rowname == "EconomicsAnti-Socialism",
                      "Anti-Socialism",
                      ifelse(rowname == "ImmigrationNo Policy",
                        "Baseline: No Immig. Policy",
                        ifelse(rowname == "ImmigrationPathway for Undocumented",
                          "Pathway for Undocumented",
                          ifelse(rowname == "ImmigrationMore Legal Immigrants",
                            "More Legal Immigrants",
                            ifelse(rowname == "ImmigrationBorder Security",
                              "Border Security",
                              ifelse(rowname == "ImmigrationAnti-Undocumented",
                                "Anti-Undocumented",
                                ifelse(rowname == "AbortionNo Policy",
                                  "Baseline: No Abortion Policy",
                                  ifelse(rowname == "AbortionLegal Without Restrictions",
                                    "Legal Without Restrictions",
                                    ifelse(rowname == "AbortionLegal to 3rd Trimester",
                                      "Legal to 3rd Trimester",
                                      ifelse(rowname == "AbortionLegal to 2nd Trimester",
                                        "Legal to 2nd Trimester",
                                        ifelse(rowname == "AbortionIllegal With Exceptions for Rape/Incest",
                                          "Illegal with Exceptions for Rape/Incest",
                                          rowname
                                        )
                                      )
                                    )
                                  )
                                )
                              )
                            )
                          )
                        )
                      )
                    )
                  )
                )
              )
            )
          )
        )
      )
    )
  ))


pid.5pt.df$`Candidate Attribute/Policy` <- factor(pid.5pt.df$`Candidate Attribute/Policy`,
  levels = c(
    "Illegal with Exceptions for Rape/Incest",
    "Legal to 2nd Trimester",
    "Legal to 3rd Trimester",
    "Legal Without Restrictions",
    "Baseline: No Abortion Policy",
    "Anti-Undocumented",
    "Border Security",
    "More Legal Immigrants",
    "Pathway for Undocumented",
    "Baseline: No Immig. Policy",
    "Anti-Socialism",
    "Inflation/Small Businesses",
    "Infrastructure",
    "Services",
    "Progressive",
    "Baseline: No Econ. Policy",
    "Latino",
    "Baseline: White",
    "Democrat",
    "Baseline: Republican"
  )
)


### Plot, point size = significances

temp.df <- pid.5pt.df %>%
  mutate(signif_size = ifelse(signif < 0.05,
    3,
    1
  ))

ggplot(
  temp.df,
  aes(
    x = Estimate,
    y = `Candidate Attribute/Policy`,
    color = `Linked Fate`
  )
) +
  geom_point(size = temp.df$signif_size) +
  geom_segment(aes(
    yend = `Candidate Attribute/Policy`,
    x = lis,
    xend = ulis
  )) +
  geom_vline(xintercept = 0, linetype = "dashed") +
  # coord_cartesian(c(-0.5, 0.65)) +
  facet_grid(attribute ~ `Linked Fate`, scales = "free_y") +
  # scale_color_manual(values = c(
  #   "blue2",
  #   "yellow4",
  #   "red4"
  # )) +
  labs(x = "Estimated Change in Vote Choice (0 or 1)")+
  theme(legend.position = "none")

ggsave("Final/Appendix/study3_bylinkedfate.jpg",
  height = 8,
  width = 12
)
```


```{r Study 3 Appendix: Spanish skills as predictor}


## Fixing factor levels

conjoint.df$Race <- factor(conjoint.df$Race,
                           levels = c("White",
                                      "Latino"))

conjoint.df$Party <- factor(conjoint.df$Party,
                            levels = c("Republican",
                                       "Democrat"))

conjoint.df$Economics <- factor(conjoint.df$Economics,
                            levels = c("No Policy",
                                       "Progressive",
                                       "Services",
                                       "Infrastructure",
                                       "Inflation/Small Businesses",
                                       "Anti-Socialism"))

conjoint.df$Immigration <- factor(conjoint.df$Immigration,
                            levels = c("No Policy",
                                       "Pathway for Undocumented",
                                       "More Legal Immigrants",
                                       "Border Security",
                                       "Anti-Undocumented"))

conjoint.df$Abortion <- factor(conjoint.df$Abortion,
                            levels = c("No Policy",
                                       "Legal Without Restrictions",
                                       "Legal to 3rd Trimester",
                                       "Legal to 2nd Trimester",
                                       "Illegal With Exceptions for Rape/Incest"))



## Heterogeneous effects by Spanish

diag_df <- conjoint.df %>%
  group_by(Spanish) %>%
  reframe(count=n())


lf1.df <- conjoint.df %>%
  filter(Spanish == "Not at all")

lf2.df <- conjoint.df %>%
  filter(Spanish == "Just a little")

lf3.df <- conjoint.df %>%
  filter(Spanish == "Somewhat well")

lf4.df <- conjoint.df %>%
  filter(Spanish == "Very well")

lf5.df <- conjoint.df %>%
  filter(Spanish == "Perfectly")


## None (lf1)

## Run LM for AMCEs

dem.lm <- lm(
  data = lf1.df,
  Vote ~ Party + Race + Economics + Immigration + Abortion
)

## Use cluster function to calculate respondent-clustered SEs

dem.lm.cl <- cl(
  dat = lf1.df,
  cluster = lf1.df$ResponseId,
  fm = dem.lm
)

dem.lm.df <- data.frame(dem.lm.cl[, ]) %>%
  rownames_to_column() %>%
  filter(rowname != "(Intercept)")

dem.lm.df <- dem.lm.df %>%
  mutate(
    lis = Estimate - 1.96 * Std..Error,
    ulis = Estimate + 1.96 * Std..Error
  )

dem.lm.df <- dem.lm.df %>%
  dplyr::select(rowname,
    Estimate,
    signif = Pr...t..,
    lis,
    ulis
  )

## Add baseline categories back

baselines.df <- data.frame(
  rowname = c(
    "Baseline: Republican",
    "Baseline: White",
    "Baseline: No Econ. Policy",
    "Baseline: No Immig. Policy",
    "Baseline: No Abortion Policy"
  ),
  Estimate = c(0, 0, 0, 0, 0),
  lis = c(0, 0, 0, 0, 0),
  ulis = c(0, 0, 0, 0, 0),
  signif = c(rep(1)),
  attribute = c(
    "Party",
    "Race",
    "Economics",
    "Immigration",
    "Abortion"
  )
)

## Add attribute for facets

dem.lm.df$attribute <- c(
  "Party",
  "Race",
  "Economics", "Economics", "Economics", "Economics", "Economics",
  "Immigration", "Immigration", "Immigration", "Immigration",
  "Abortion", "Abortion", "Abortion", "Abortion"
)

plot1.df <- rbind(
  dem.lm.df,
  baselines.df
)

## Add party ID variable

plot1.df$Spanish <- c(rep("Not at all"))


## A little

## Run LM for AMCEs

dem.lm <- lm(
  data = lf2.df,
  Vote ~ Party + Race + Economics + Immigration + Abortion
)

## Use cluster function to calculate respondent-clustered SEs

dem.lm.cl <- cl(
  dat = lf2.df,
  cluster = lf2.df$ResponseId,
  fm = dem.lm
)

dem.lm.df <- data.frame(dem.lm.cl[, ]) %>%
  rownames_to_column() %>%
  filter(rowname != "(Intercept)")

dem.lm.df <- dem.lm.df %>%
  mutate(
    lis = Estimate - 1.96 * Std..Error,
    ulis = Estimate + 1.96 * Std..Error
  )

dem.lm.df <- dem.lm.df %>%
  dplyr::select(rowname,
    Estimate,
    signif = Pr...t..,
    lis,
    ulis
  )

## Add baseline categories back

baselines.df <- data.frame(
  rowname = c(
    "Baseline: Republican",
    "Baseline: White",
    "Baseline: No Econ. Policy",
    "Baseline: No Immig. Policy",
    "Baseline: No Abortion Policy"
  ),
  Estimate = c(0, 0, 0, 0, 0),
  lis = c(0, 0, 0, 0, 0),
  ulis = c(0, 0, 0, 0, 0),
  signif = c(rep(1)),
  attribute = c(
    "Party",
    "Race",
    "Economics",
    "Immigration",
    "Abortion"
  )
)

## Add attribute for facets

dem.lm.df$attribute <- c(
  "Party",
  "Race",
  "Economics", "Economics", "Economics", "Economics", "Economics",
  "Immigration", "Immigration", "Immigration", "Immigration",
  "Abortion", "Abortion", "Abortion", "Abortion"
)

plot2.df <- rbind(
  dem.lm.df,
  baselines.df
)

## Add party ID variable

plot2.df$Spanish <- c(rep("Just a little"))


## Moderate

## Run LM for AMCEs

dem.lm <- lm(
  data = lf3.df,
  Vote ~ Party + Race + Economics + Immigration + Abortion
)

## Use cluster function to calculate respondent-clustered SEs

dem.lm.cl <- cl(
  dat = lf3.df,
  cluster = lf3.df$ResponseId,
  fm = dem.lm
)

dem.lm.df <- data.frame(dem.lm.cl[, ]) %>%
  rownames_to_column() %>%
  filter(rowname != "(Intercept)")

dem.lm.df <- dem.lm.df %>%
  mutate(
    lis = Estimate - 1.96 * Std..Error,
    ulis = Estimate + 1.96 * Std..Error
  )

dem.lm.df <- dem.lm.df %>%
  dplyr::select(rowname,
    Estimate,
    signif = Pr...t..,
    lis,
    ulis
  )

## Add baseline categories back

baselines.df <- data.frame(
  rowname = c(
    "Baseline: Republican",
    "Baseline: White",
    "Baseline: No Econ. Policy",
    "Baseline: No Immig. Policy",
    "Baseline: No Abortion Policy"
  ),
  Estimate = c(0, 0, 0, 0, 0),
  lis = c(0, 0, 0, 0, 0),
  ulis = c(0, 0, 0, 0, 0),
  signif = c(rep(1)),
  attribute = c(
    "Party",
    "Race",
    "Economics",
    "Immigration",
    "Abortion"
  )
)

## Add attribute for facets

dem.lm.df$attribute <- c(
  "Party",
  "Race",
  "Economics", "Economics", "Economics", "Economics", "Economics",
  "Immigration", "Immigration", "Immigration", "Immigration",
  "Abortion", "Abortion", "Abortion", "Abortion"
)

plot3.df <- rbind(
  dem.lm.df,
  baselines.df
)

## Add party ID variable

plot3.df$Spanish <- c(rep("Somewhat well"))


## A lot

## Run LM for AMCEs

dem.lm <- lm(
  data = lf4.df,
  Vote ~ Party + Race + Economics + Immigration + Abortion
)

## Use cluster function to calculate respondent-clustered SEs

dem.lm.cl <- cl(
  dat = lf4.df,
  cluster = lf4.df$ResponseId,
  fm = dem.lm
)

dem.lm.df <- data.frame(dem.lm.cl[, ]) %>%
  rownames_to_column() %>%
  filter(rowname != "(Intercept)")

dem.lm.df <- dem.lm.df %>%
  mutate(
    lis = Estimate - 1.96 * Std..Error,
    ulis = Estimate + 1.96 * Std..Error
  )

dem.lm.df <- dem.lm.df %>%
  dplyr::select(rowname,
    Estimate,
    signif = Pr...t..,
    lis,
    ulis
  )


## Add baseline categories back

baselines.df <- data.frame(
  rowname = c(
    "Baseline: Republican",
    "Baseline: White",
    "Baseline: No Econ. Policy",
    "Baseline: No Immig. Policy",
    "Baseline: No Abortion Policy"
  ),
  Estimate = c(0, 0, 0, 0, 0),
  lis = c(0, 0, 0, 0, 0),
  ulis = c(0, 0, 0, 0, 0),
  signif = c(rep(1)),
  attribute = c(
    "Party",
    "Race",
    "Economics",
    "Immigration",
    "Abortion"
  )
)


## Add attribute for facets

dem.lm.df$attribute <- c(
  "Party",
  "Race",
  "Economics", "Economics", "Economics", "Economics", "Economics",
  "Immigration", "Immigration", "Immigration", "Immigration",
  "Abortion", "Abortion", "Abortion", "Abortion"
)

plot4.df <- rbind(
  dem.lm.df,
  baselines.df
)

## Add party ID variable

plot4.df$Spanish <- c(rep("Very well"))


## A great deal

## Run LM for AMCEs

dem.lm <- lm(
  data = lf5.df,
  Vote ~ Party + Race + Economics + Immigration + Abortion
)

## Use cluster function to calculate respondent-clustered SEs

dem.lm.cl <- cl(
  dat = lf5.df,
  cluster = lf5.df$ResponseId,
  fm = dem.lm
)

dem.lm.df <- data.frame(dem.lm.cl[, ]) %>%
  rownames_to_column() %>%
  filter(rowname != "(Intercept)")

dem.lm.df <- dem.lm.df %>%
  mutate(
    lis = Estimate - 1.96 * Std..Error,
    ulis = Estimate + 1.96 * Std..Error
  )

dem.lm.df <- dem.lm.df %>%
  dplyr::select(rowname,
    Estimate,
    signif = Pr...t..,
    lis,
    ulis
  )

## Add baseline categories back

baselines.df <- data.frame(
  rowname = c(
    "Baseline: Republican",
    "Baseline: White",
    "Baseline: No Econ. Policy",
    "Baseline: No Immig. Policy",
    "Baseline: No Abortion Policy"
  ),
  Estimate = c(0, 0, 0, 0, 0),
  lis = c(0, 0, 0, 0, 0),
  ulis = c(0, 0, 0, 0, 0),
  signif = c(rep(1)),
  attribute = c(
    "Party",
    "Race",
    "Economics",
    "Immigration",
    "Abortion"
  )
)

## Add attribute for facets

dem.lm.df$attribute <- c(
  "Party",
  "Race",
  "Economics", "Economics", "Economics", "Economics", "Economics",
  "Immigration", "Immigration", "Immigration", "Immigration",
  "Abortion", "Abortion", "Abortion", "Abortion"
)

plot5.df <- rbind(
  dem.lm.df,
  baselines.df
)

## Add party ID variable

plot5.df$Spanish <- c(rep("Perfectly"))



### All five combined

## First combine with baselines

pid.5pt.df <- rbind(
  plot1.df,
  plot2.df,
  plot3.df,
  plot4.df,
  plot5.df
)

pid.5pt.df$Spanish <- factor(pid.5pt.df$Spanish,
  levels = c(
    "Not at all",
    "Just a little",
    "Somewhat well",
    "Very well",
    "Perfectly"
  )
)


## Fix factor levels

pid.5pt.df$attribute <- factor(pid.5pt.df$attribute,
  levels = c(
    "Party",
    "Race",
    "Economics",
    "Immigration",
    "Abortion"
  )
)

## Fix row names and factor level

pid.5pt.df <- pid.5pt.df %>%
  mutate(`Candidate Attribute/Policy` = ifelse(rowname == "PartyDemocrat",
    "Democrat",
    ifelse(rowname == "PartyRepublican",
      "Baseline: Republican",
      ifelse(rowname == "RaceLatino",
        "Latino",
        ifelse(rowname == "RaceWhite",
          "Baseline: White",
          ifelse(rowname == "EconomicsNo Policy",
            "Baseline: No Econ. Policy",
            ifelse(rowname == "EconomicsProgressive",
              "Progressive",
              ifelse(rowname == "EconomicsServices",
                "Services",
                ifelse(rowname == "EconomicsInfrastructure",
                  "Infrastructure",
                  ifelse(rowname == "EconomicsInflation/Small Businesses",
                    "Inflation/Small Businesses",
                    ifelse(rowname == "EconomicsAnti-Socialism",
                      "Anti-Socialism",
                      ifelse(rowname == "ImmigrationNo Policy",
                        "Baseline: No Immig. Policy",
                        ifelse(rowname == "ImmigrationPathway for Undocumented",
                          "Pathway for Undocumented",
                          ifelse(rowname == "ImmigrationMore Legal Immigrants",
                            "More Legal Immigrants",
                            ifelse(rowname == "ImmigrationBorder Security",
                              "Border Security",
                              ifelse(rowname == "ImmigrationAnti-Undocumented",
                                "Anti-Undocumented",
                                ifelse(rowname == "AbortionNo Policy",
                                  "Baseline: No Abortion Policy",
                                  ifelse(rowname == "AbortionLegal Without Restrictions",
                                    "Legal Without Restrictions",
                                    ifelse(rowname == "AbortionLegal to 3rd Trimester",
                                      "Legal to 3rd Trimester",
                                      ifelse(rowname == "AbortionLegal to 2nd Trimester",
                                        "Legal to 2nd Trimester",
                                        ifelse(rowname == "AbortionIllegal With Exceptions for Rape/Incest",
                                          "Illegal with Exceptions for Rape/Incest",
                                          rowname
                                        )
                                      )
                                    )
                                  )
                                )
                              )
                            )
                          )
                        )
                      )
                    )
                  )
                )
              )
            )
          )
        )
      )
    )
  ))


pid.5pt.df$`Candidate Attribute/Policy` <- factor(pid.5pt.df$`Candidate Attribute/Policy`,
  levels = c(
    "Illegal with Exceptions for Rape/Incest",
    "Legal to 2nd Trimester",
    "Legal to 3rd Trimester",
    "Legal Without Restrictions",
    "Baseline: No Abortion Policy",
    "Anti-Undocumented",
    "Border Security",
    "More Legal Immigrants",
    "Pathway for Undocumented",
    "Baseline: No Immig. Policy",
    "Anti-Socialism",
    "Inflation/Small Businesses",
    "Infrastructure",
    "Services",
    "Progressive",
    "Baseline: No Econ. Policy",
    "Latino",
    "Baseline: White",
    "Democrat",
    "Baseline: Republican"
  )
)


### Plot, point size = significances

temp.df <- pid.5pt.df %>%
  mutate(signif_size = ifelse(signif < 0.05,
    3,
    1
  ))

ggplot(
  temp.df,
  aes(
    x = Estimate,
    y = `Candidate Attribute/Policy`,
    color = Spanish
  )
) +
  geom_point(size = temp.df$signif_size) +
  geom_segment(aes(
    yend = `Candidate Attribute/Policy`,
    x = lis,
    xend = ulis
  )) +
  geom_vline(xintercept = 0, linetype = "dashed") +
  # coord_cartesian(c(-0.5, 0.65)) +
  facet_grid(attribute ~ Spanish, scales = "free_y") +
  # scale_color_manual(values = c(
  #   "blue2",
  #   "yellow4",
  #   "red4"
  # )) +
  labs(x = "Estimated Change in Vote Choice (0 or 1)")+
  theme(legend.position = "none")

ggsave("Final/Appendix/study3_byspanishskills.jpg",
  height = 8,
  width = 12
)
```



```{r Study 3 Appendix: Race as predictor}

## Fixing factor levels

conjoint.df$Race <- factor(conjoint.df$Race,
                           levels = c("White",
                                      "Latino"))

conjoint.df$Party <- factor(conjoint.df$Party,
                            levels = c("Republican",
                                       "Democrat"))

conjoint.df$Economics <- factor(conjoint.df$Economics,
                            levels = c("No Policy",
                                       "Progressive",
                                       "Services",
                                       "Infrastructure",
                                       "Inflation/Small Businesses",
                                       "Anti-Socialism"))

conjoint.df$Immigration <- factor(conjoint.df$Immigration,
                            levels = c("No Policy",
                                       "Pathway for Undocumented",
                                       "More Legal Immigrants",
                                       "Border Security",
                                       "Anti-Undocumented"))

conjoint.df$Abortion <- factor(conjoint.df$Abortion,
                            levels = c("No Policy",
                                       "Legal Without Restrictions",
                                       "Legal to 3rd Trimester",
                                       "Legal to 2nd Trimester",
                                       "Illegal With Exceptions for Rape/Incest"))



## Heterogeneous effects by Linked Fate

diag_df <- conjoint.df %>%
  group_by(R.Race) %>%
  reframe(count=n())


lf1.df <- conjoint.df %>%
  filter(R.Race == "Only Latino")

lf2.df <- conjoint.df %>%
  filter(R.Race == "Latino and White")

lf3.df <- conjoint.df %>%
  filter(R.Race == "Latino and Black")

lf4.df <- conjoint.df %>%
  filter(R.Race == "Other/Multiracial")


## Only Latino

## Run LM for AMCEs

dem.lm <- lm(
  data = lf1.df,
  Vote ~ Party + Race + Economics + Immigration + Abortion
)

## Use cluster function to calculate respondent-clustered SEs

dem.lm.cl <- cl(
  dat = lf1.df,
  cluster = lf1.df$ResponseId,
  fm = dem.lm
)

dem.lm.df <- data.frame(dem.lm.cl[, ]) %>%
  rownames_to_column() %>%
  filter(rowname != "(Intercept)")

dem.lm.df <- dem.lm.df %>%
  mutate(
    lis = Estimate - 1.96 * Std..Error,
    ulis = Estimate + 1.96 * Std..Error
  )

dem.lm.df <- dem.lm.df %>%
  dplyr::select(rowname,
    Estimate,
    signif = Pr...t..,
    lis,
    ulis
  )

## Add baseline categories back

baselines.df <- data.frame(
  rowname = c(
    "Baseline: Republican",
    "Baseline: White",
    "Baseline: No Econ. Policy",
    "Baseline: No Immig. Policy",
    "Baseline: No Abortion Policy"
  ),
  Estimate = c(0, 0, 0, 0, 0),
  lis = c(0, 0, 0, 0, 0),
  ulis = c(0, 0, 0, 0, 0),
  signif = c(rep(1)),
  attribute = c(
    "Party",
    "Race",
    "Economics",
    "Immigration",
    "Abortion"
  )
)

## Add attribute for facets

dem.lm.df$attribute <- c(
  "Party",
  "Race",
  "Economics", "Economics", "Economics", "Economics", "Economics",
  "Immigration", "Immigration", "Immigration", "Immigration",
  "Abortion", "Abortion", "Abortion", "Abortion"
)

plot1.df <- rbind(
  dem.lm.df,
  baselines.df
)

## Add party ID variable

plot1.df$Race <- c(rep("Only Latino"))

## Latino and White

## Run LM for AMCEs

dem.lm <- lm(
  data = lf2.df,
  Vote ~ Party + Race + Economics + Immigration + Abortion
)

## Use cluster function to calculate respondent-clustered SEs

dem.lm.cl <- cl(
  dat = lf2.df,
  cluster = lf2.df$ResponseId,
  fm = dem.lm
)

dem.lm.df <- data.frame(dem.lm.cl[, ]) %>%
  rownames_to_column() %>%
  filter(rowname != "(Intercept)")

dem.lm.df <- dem.lm.df %>%
  mutate(
    lis = Estimate - 1.96 * Std..Error,
    ulis = Estimate + 1.96 * Std..Error
  )

dem.lm.df <- dem.lm.df %>%
  dplyr::select(rowname,
    Estimate,
    signif = Pr...t..,
    lis,
    ulis
  )

## Add baseline categories back

baselines.df <- data.frame(
  rowname = c(
    "Baseline: Republican",
    "Baseline: White",
    "Baseline: No Econ. Policy",
    "Baseline: No Immig. Policy",
    "Baseline: No Abortion Policy"
  ),
  Estimate = c(0, 0, 0, 0, 0),
  lis = c(0, 0, 0, 0, 0),
  ulis = c(0, 0, 0, 0, 0),
  signif = c(rep(1)),
  attribute = c(
    "Party",
    "Race",
    "Economics",
    "Immigration",
    "Abortion"
  )
)

## Add attribute for facets

dem.lm.df$attribute <- c(
  "Party",
  "Race",
  "Economics", "Economics", "Economics", "Economics", "Economics",
  "Immigration", "Immigration", "Immigration", "Immigration",
  "Abortion", "Abortion", "Abortion", "Abortion"
)

plot2.df <- rbind(
  dem.lm.df,
  baselines.df
)

## Add party ID variable

plot2.df$Race <- c(rep("Latino and White"))


## Moderate

## Run LM for AMCEs

dem.lm <- lm(
  data = lf3.df,
  Vote ~ Party + Race + Economics + Immigration + Abortion
)

## Use cluster function to calculate respondent-clustered SEs

dem.lm.cl <- cl(
  dat = lf3.df,
  cluster = lf3.df$ResponseId,
  fm = dem.lm
)

dem.lm.df <- data.frame(dem.lm.cl[, ]) %>%
  rownames_to_column() %>%
  filter(rowname != "(Intercept)")

dem.lm.df <- dem.lm.df %>%
  mutate(
    lis = Estimate - 1.96 * Std..Error,
    ulis = Estimate + 1.96 * Std..Error
  )

dem.lm.df <- dem.lm.df %>%
  dplyr::select(rowname,
    Estimate,
    signif = Pr...t..,
    lis,
    ulis
  )

## Add baseline categories back

baselines.df <- data.frame(
  rowname = c(
    "Baseline: Republican",
    "Baseline: White",
    "Baseline: No Econ. Policy",
    "Baseline: No Immig. Policy",
    "Baseline: No Abortion Policy"
  ),
  Estimate = c(0, 0, 0, 0, 0),
  lis = c(0, 0, 0, 0, 0),
  ulis = c(0, 0, 0, 0, 0),
  signif = c(rep(1)),
  attribute = c(
    "Party",
    "Race",
    "Economics",
    "Immigration",
    "Abortion"
  )
)

## Add attribute for facets

dem.lm.df$attribute <- c(
  "Party",
  "Race",
  "Economics", "Economics", "Economics", "Economics", "Economics",
  "Immigration", "Immigration", "Immigration", "Immigration",
  "Abortion", "Abortion", "Abortion", "Abortion"
)

plot3.df <- rbind(
  dem.lm.df,
  baselines.df
)

## Add party ID variable

plot3.df$Race <- c(rep("Latino and Black"))


## Other/Multiracial

## Run LM for AMCEs

dem.lm <- lm(
  data = lf4.df,
  Vote ~ Party + Race + Economics + Immigration + Abortion
)

## Use cluster function to calculate respondent-clustered SEs

dem.lm.cl <- cl(
  dat = lf4.df,
  cluster = lf4.df$ResponseId,
  fm = dem.lm
)

dem.lm.df <- data.frame(dem.lm.cl[, ]) %>%
  rownames_to_column() %>%
  filter(rowname != "(Intercept)")

dem.lm.df <- dem.lm.df %>%
  mutate(
    lis = Estimate - 1.96 * Std..Error,
    ulis = Estimate + 1.96 * Std..Error
  )

dem.lm.df <- dem.lm.df %>%
  dplyr::select(rowname,
    Estimate,
    signif = Pr...t..,
    lis,
    ulis
  )


## Add baseline categories back

baselines.df <- data.frame(
  rowname = c(
    "Baseline: Republican",
    "Baseline: White",
    "Baseline: No Econ. Policy",
    "Baseline: No Immig. Policy",
    "Baseline: No Abortion Policy"
  ),
  Estimate = c(0, 0, 0, 0, 0),
  lis = c(0, 0, 0, 0, 0),
  ulis = c(0, 0, 0, 0, 0),
  signif = c(rep(1)),
  attribute = c(
    "Party",
    "Race",
    "Economics",
    "Immigration",
    "Abortion"
  )
)


## Add attribute for facets

dem.lm.df$attribute <- c(
  "Party",
  "Race",
  "Economics", "Economics", "Economics", "Economics", "Economics",
  "Immigration", "Immigration", "Immigration", "Immigration",
  "Abortion", "Abortion", "Abortion", "Abortion"
)

plot4.df <- rbind(
  dem.lm.df,
  baselines.df
)

## Add party ID variable

plot4.df$Race <- c(rep("Other/Multiracial"))



### All four combined

## First combine with baselines

pid.5pt.df <- rbind(
  plot1.df,
  plot2.df,
  plot3.df,
  plot4.df
)

pid.5pt.df$Race<- factor(pid.5pt.df$Race,
  levels = c(
    "Only Latino",
    "Latino and White",
    "Latino and Black",
    "Other/Multiracial"
  )
)


## Fix factor levels

pid.5pt.df$attribute <- factor(pid.5pt.df$attribute,
  levels = c(
    "Party",
    "Race",
    "Economics",
    "Immigration",
    "Abortion"
  )
)

## Fix row names and factor level

pid.5pt.df <- pid.5pt.df %>%
  mutate(`Candidate Attribute/Policy` = ifelse(rowname == "PartyDemocrat",
    "Democrat",
    ifelse(rowname == "PartyRepublican",
      "Baseline: Republican",
      ifelse(rowname == "RaceLatino",
        "Latino",
        ifelse(rowname == "RaceWhite",
          "Baseline: White",
          ifelse(rowname == "EconomicsNo Policy",
            "Baseline: No Econ. Policy",
            ifelse(rowname == "EconomicsProgressive",
              "Progressive",
              ifelse(rowname == "EconomicsServices",
                "Services",
                ifelse(rowname == "EconomicsInfrastructure",
                  "Infrastructure",
                  ifelse(rowname == "EconomicsInflation/Small Businesses",
                    "Inflation/Small Businesses",
                    ifelse(rowname == "EconomicsAnti-Socialism",
                      "Anti-Socialism",
                      ifelse(rowname == "ImmigrationNo Policy",
                        "Baseline: No Immig. Policy",
                        ifelse(rowname == "ImmigrationPathway for Undocumented",
                          "Pathway for Undocumented",
                          ifelse(rowname == "ImmigrationMore Legal Immigrants",
                            "More Legal Immigrants",
                            ifelse(rowname == "ImmigrationBorder Security",
                              "Border Security",
                              ifelse(rowname == "ImmigrationAnti-Undocumented",
                                "Anti-Undocumented",
                                ifelse(rowname == "AbortionNo Policy",
                                  "Baseline: No Abortion Policy",
                                  ifelse(rowname == "AbortionLegal Without Restrictions",
                                    "Legal Without Restrictions",
                                    ifelse(rowname == "AbortionLegal to 3rd Trimester",
                                      "Legal to 3rd Trimester",
                                      ifelse(rowname == "AbortionLegal to 2nd Trimester",
                                        "Legal to 2nd Trimester",
                                        ifelse(rowname == "AbortionIllegal With Exceptions for Rape/Incest",
                                          "Illegal with Exceptions for Rape/Incest",
                                          rowname
                                        )
                                      )
                                    )
                                  )
                                )
                              )
                            )
                          )
                        )
                      )
                    )
                  )
                )
              )
            )
          )
        )
      )
    )
  ))


pid.5pt.df$`Candidate Attribute/Policy` <- factor(pid.5pt.df$`Candidate Attribute/Policy`,
  levels = c(
    "Illegal with Exceptions for Rape/Incest",
    "Legal to 2nd Trimester",
    "Legal to 3rd Trimester",
    "Legal Without Restrictions",
    "Baseline: No Abortion Policy",
    "Anti-Undocumented",
    "Border Security",
    "More Legal Immigrants",
    "Pathway for Undocumented",
    "Baseline: No Immig. Policy",
    "Anti-Socialism",
    "Inflation/Small Businesses",
    "Infrastructure",
    "Services",
    "Progressive",
    "Baseline: No Econ. Policy",
    "Latino",
    "Baseline: White",
    "Democrat",
    "Baseline: Republican"
  )
)


### Plot, point size = significances

temp.df <- pid.5pt.df %>%
  mutate(signif_size = ifelse(signif < 0.05,
    3,
    1
  ))

ggplot(
  temp.df,
  aes(
    x = Estimate,
    y = `Candidate Attribute/Policy`,
    color = Race
  )
) +
  geom_point(size = temp.df$signif_size) +
  geom_segment(aes(
    yend = `Candidate Attribute/Policy`,
    x = lis,
    xend = ulis
  )) +
  geom_vline(xintercept = 0, linetype = "dashed") +
  # coord_cartesian(c(-0.5, 0.65)) +
  facet_grid(attribute ~ Race, scales = "free_y") +
  # scale_color_manual(values = c(
  #   "blue2",
  #   "yellow4",
  #   "red4"
  # )) +
  labs(x = "Estimated Change in Vote Choice (0 or 1)")+
  theme(legend.position = "none")

ggsave("Final/Appendix/study3_byrace.jpg",
  height = 8,
  width = 12
)
```



```{r Study 3 Appendix: Natl origin as predictor}

## Fixing factor levels

conjoint.df$Race <- factor(conjoint.df$Race,
                           levels = c("White",
                                      "Latino"))

conjoint.df$Party <- factor(conjoint.df$Party,
                            levels = c("Republican",
                                       "Democrat"))

conjoint.df$Economics <- factor(conjoint.df$Economics,
                            levels = c("No Policy",
                                       "Progressive",
                                       "Services",
                                       "Infrastructure",
                                       "Inflation/Small Businesses",
                                       "Anti-Socialism"))

conjoint.df$Immigration <- factor(conjoint.df$Immigration,
                            levels = c("No Policy",
                                       "Pathway for Undocumented",
                                       "More Legal Immigrants",
                                       "Border Security",
                                       "Anti-Undocumented"))

conjoint.df$Abortion <- factor(conjoint.df$Abortion,
                            levels = c("No Policy",
                                       "Legal Without Restrictions",
                                       "Legal to 3rd Trimester",
                                       "Legal to 2nd Trimester",
                                       "Illegal With Exceptions for Rape/Incest"))



## Heterogeneous effects by Linked Fate

diag_df <- conjoint.df %>%
  group_by(Origin) %>%
  reframe(count=n())


lf1.df <- conjoint.df %>%
  filter(Origin == "Mexico")

lf2.df <- conjoint.df %>%
  filter(Origin == "Puerto Rico")

lf3.df <- conjoint.df %>%
  filter(Origin == "Cuba")

lf4.df <- conjoint.df %>%
  filter(Origin == "Other")


## Only Latino

## Run LM for AMCEs

dem.lm <- lm(
  data = lf1.df,
  Vote ~ Party + Race + Economics + Immigration + Abortion
)

## Use cluster function to calculate respondent-clustered SEs

dem.lm.cl <- cl(
  dat = lf1.df,
  cluster = lf1.df$ResponseId,
  fm = dem.lm
)

dem.lm.df <- data.frame(dem.lm.cl[, ]) %>%
  rownames_to_column() %>%
  filter(rowname != "(Intercept)")

dem.lm.df <- dem.lm.df %>%
  mutate(
    lis = Estimate - 1.96 * Std..Error,
    ulis = Estimate + 1.96 * Std..Error
  )

dem.lm.df <- dem.lm.df %>%
  dplyr::select(rowname,
    Estimate,
    signif = Pr...t..,
    lis,
    ulis
  )

## Add baseline categories back

baselines.df <- data.frame(
  rowname = c(
    "Baseline: Republican",
    "Baseline: White",
    "Baseline: No Econ. Policy",
    "Baseline: No Immig. Policy",
    "Baseline: No Abortion Policy"
  ),
  Estimate = c(0, 0, 0, 0, 0),
  lis = c(0, 0, 0, 0, 0),
  ulis = c(0, 0, 0, 0, 0),
  signif = c(rep(1)),
  attribute = c(
    "Party",
    "Race",
    "Economics",
    "Immigration",
    "Abortion"
  )
)

## Add attribute for facets

dem.lm.df$attribute <- c(
  "Party",
  "Race",
  "Economics", "Economics", "Economics", "Economics", "Economics",
  "Immigration", "Immigration", "Immigration", "Immigration",
  "Abortion", "Abortion", "Abortion", "Abortion"
)

plot1.df <- rbind(
  dem.lm.df,
  baselines.df
)

## Add party ID variable

plot1.df$`National Origin` <- c(rep("Mexico"))

## Latino and White

## Run LM for AMCEs

dem.lm <- lm(
  data = lf2.df,
  Vote ~ Party + Race + Economics + Immigration + Abortion
)

## Use cluster function to calculate respondent-clustered SEs

dem.lm.cl <- cl(
  dat = lf2.df,
  cluster = lf2.df$ResponseId,
  fm = dem.lm
)

dem.lm.df <- data.frame(dem.lm.cl[, ]) %>%
  rownames_to_column() %>%
  filter(rowname != "(Intercept)")

dem.lm.df <- dem.lm.df %>%
  mutate(
    lis = Estimate - 1.96 * Std..Error,
    ulis = Estimate + 1.96 * Std..Error
  )

dem.lm.df <- dem.lm.df %>%
  dplyr::select(rowname,
    Estimate,
    signif = Pr...t..,
    lis,
    ulis
  )

## Add baseline categories back

baselines.df <- data.frame(
  rowname = c(
    "Baseline: Republican",
    "Baseline: White",
    "Baseline: No Econ. Policy",
    "Baseline: No Immig. Policy",
    "Baseline: No Abortion Policy"
  ),
  Estimate = c(0, 0, 0, 0, 0),
  lis = c(0, 0, 0, 0, 0),
  ulis = c(0, 0, 0, 0, 0),
  signif = c(rep(1)),
  attribute = c(
    "Party",
    "Race",
    "Economics",
    "Immigration",
    "Abortion"
  )
)

## Add attribute for facets

dem.lm.df$attribute <- c(
  "Party",
  "Race",
  "Economics", "Economics", "Economics", "Economics", "Economics",
  "Immigration", "Immigration", "Immigration", "Immigration",
  "Abortion", "Abortion", "Abortion", "Abortion"
)

plot2.df <- rbind(
  dem.lm.df,
  baselines.df
)

## Add party ID variable

plot2.df$`National Origin` <- c(rep("Puerto Rico"))


## Moderate

## Run LM for AMCEs

dem.lm <- lm(
  data = lf3.df,
  Vote ~ Party + Race + Economics + Immigration + Abortion
)

## Use cluster function to calculate respondent-clustered SEs

dem.lm.cl <- cl(
  dat = lf3.df,
  cluster = lf3.df$ResponseId,
  fm = dem.lm
)

dem.lm.df <- data.frame(dem.lm.cl[, ]) %>%
  rownames_to_column() %>%
  filter(rowname != "(Intercept)")

dem.lm.df <- dem.lm.df %>%
  mutate(
    lis = Estimate - 1.96 * Std..Error,
    ulis = Estimate + 1.96 * Std..Error
  )

dem.lm.df <- dem.lm.df %>%
  dplyr::select(rowname,
    Estimate,
    signif = Pr...t..,
    lis,
    ulis
  )

## Add baseline categories back

baselines.df <- data.frame(
  rowname = c(
    "Baseline: Republican",
    "Baseline: White",
    "Baseline: No Econ. Policy",
    "Baseline: No Immig. Policy",
    "Baseline: No Abortion Policy"
  ),
  Estimate = c(0, 0, 0, 0, 0),
  lis = c(0, 0, 0, 0, 0),
  ulis = c(0, 0, 0, 0, 0),
  signif = c(rep(1)),
  attribute = c(
    "Party",
    "Race",
    "Economics",
    "Immigration",
    "Abortion"
  )
)

## Add attribute for facets

dem.lm.df$attribute <- c(
  "Party",
  "Race",
  "Economics", "Economics", "Economics", "Economics", "Economics",
  "Immigration", "Immigration", "Immigration", "Immigration",
  "Abortion", "Abortion", "Abortion", "Abortion"
)

plot3.df <- rbind(
  dem.lm.df,
  baselines.df
)

## Add party ID variable

plot3.df$`National Origin` <- c(rep("Cuba"))


## Other/Multiracial

## Run LM for AMCEs

dem.lm <- lm(
  data = lf4.df,
  Vote ~ Party + Race + Economics + Immigration + Abortion
)

## Use cluster function to calculate respondent-clustered SEs

dem.lm.cl <- cl(
  dat = lf4.df,
  cluster = lf4.df$ResponseId,
  fm = dem.lm
)

dem.lm.df <- data.frame(dem.lm.cl[, ]) %>%
  rownames_to_column() %>%
  filter(rowname != "(Intercept)")

dem.lm.df <- dem.lm.df %>%
  mutate(
    lis = Estimate - 1.96 * Std..Error,
    ulis = Estimate + 1.96 * Std..Error
  )

dem.lm.df <- dem.lm.df %>%
  dplyr::select(rowname,
    Estimate,
    signif = Pr...t..,
    lis,
    ulis
  )


## Add baseline categories back

baselines.df <- data.frame(
  rowname = c(
    "Baseline: Republican",
    "Baseline: White",
    "Baseline: No Econ. Policy",
    "Baseline: No Immig. Policy",
    "Baseline: No Abortion Policy"
  ),
  Estimate = c(0, 0, 0, 0, 0),
  lis = c(0, 0, 0, 0, 0),
  ulis = c(0, 0, 0, 0, 0),
  signif = c(rep(1)),
  attribute = c(
    "Party",
    "Race",
    "Economics",
    "Immigration",
    "Abortion"
  )
)


## Add attribute for facets

dem.lm.df$attribute <- c(
  "Party",
  "Race",
  "Economics", "Economics", "Economics", "Economics", "Economics",
  "Immigration", "Immigration", "Immigration", "Immigration",
  "Abortion", "Abortion", "Abortion", "Abortion"
)

plot4.df <- rbind(
  dem.lm.df,
  baselines.df
)

## Add party ID variable

plot4.df$`National Origin` <- c(rep("Other"))



### All four combined

## First combine with baselines

pid.5pt.df <- rbind(
  plot1.df,
  plot2.df,
  plot3.df,
  plot4.df
)

pid.5pt.df$`National Origin`<- factor(pid.5pt.df$`National Origin`,
  levels = c(
    "Mexico",
    "Puerto Rico",
    "Cuba",
    "Other"
  )
)


## Fix factor levels

pid.5pt.df$attribute <- factor(pid.5pt.df$attribute,
  levels = c(
    "Party",
    "Race",
    "Economics",
    "Immigration",
    "Abortion"
  )
)

## Fix row names and factor level

pid.5pt.df <- pid.5pt.df %>%
  mutate(`Candidate Attribute/Policy` = ifelse(rowname == "PartyDemocrat",
    "Democrat",
    ifelse(rowname == "PartyRepublican",
      "Baseline: Republican",
      ifelse(rowname == "RaceLatino",
        "Latino",
        ifelse(rowname == "RaceWhite",
          "Baseline: White",
          ifelse(rowname == "EconomicsNo Policy",
            "Baseline: No Econ. Policy",
            ifelse(rowname == "EconomicsProgressive",
              "Progressive",
              ifelse(rowname == "EconomicsServices",
                "Services",
                ifelse(rowname == "EconomicsInfrastructure",
                  "Infrastructure",
                  ifelse(rowname == "EconomicsInflation/Small Businesses",
                    "Inflation/Small Businesses",
                    ifelse(rowname == "EconomicsAnti-Socialism",
                      "Anti-Socialism",
                      ifelse(rowname == "ImmigrationNo Policy",
                        "Baseline: No Immig. Policy",
                        ifelse(rowname == "ImmigrationPathway for Undocumented",
                          "Pathway for Undocumented",
                          ifelse(rowname == "ImmigrationMore Legal Immigrants",
                            "More Legal Immigrants",
                            ifelse(rowname == "ImmigrationBorder Security",
                              "Border Security",
                              ifelse(rowname == "ImmigrationAnti-Undocumented",
                                "Anti-Undocumented",
                                ifelse(rowname == "AbortionNo Policy",
                                  "Baseline: No Abortion Policy",
                                  ifelse(rowname == "AbortionLegal Without Restrictions",
                                    "Legal Without Restrictions",
                                    ifelse(rowname == "AbortionLegal to 3rd Trimester",
                                      "Legal to 3rd Trimester",
                                      ifelse(rowname == "AbortionLegal to 2nd Trimester",
                                        "Legal to 2nd Trimester",
                                        ifelse(rowname == "AbortionIllegal With Exceptions for Rape/Incest",
                                          "Illegal with Exceptions for Rape/Incest",
                                          rowname
                                        )
                                      )
                                    )
                                  )
                                )
                              )
                            )
                          )
                        )
                      )
                    )
                  )
                )
              )
            )
          )
        )
      )
    )
  ))


pid.5pt.df$`Candidate Attribute/Policy` <- factor(pid.5pt.df$`Candidate Attribute/Policy`,
  levels = c(
    "Illegal with Exceptions for Rape/Incest",
    "Legal to 2nd Trimester",
    "Legal to 3rd Trimester",
    "Legal Without Restrictions",
    "Baseline: No Abortion Policy",
    "Anti-Undocumented",
    "Border Security",
    "More Legal Immigrants",
    "Pathway for Undocumented",
    "Baseline: No Immig. Policy",
    "Anti-Socialism",
    "Inflation/Small Businesses",
    "Infrastructure",
    "Services",
    "Progressive",
    "Baseline: No Econ. Policy",
    "Latino",
    "Baseline: White",
    "Democrat",
    "Baseline: Republican"
  )
)


### Plot, point size = significances

temp.df <- pid.5pt.df %>%
  mutate(signif_size = ifelse(signif < 0.05,
    3,
    1
  ))

ggplot(
  temp.df,
  aes(
    x = Estimate,
    y = `Candidate Attribute/Policy`,
    color = `National Origin`
  )
) +
  geom_point(size = temp.df$signif_size) +
  geom_segment(aes(
    yend = `Candidate Attribute/Policy`,
    x = lis,
    xend = ulis
  )) +
  geom_vline(xintercept = 0, linetype = "dashed") +
  # coord_cartesian(c(-0.5, 0.65)) +
  facet_grid(attribute ~ `National Origin`, scales = "free_y") +
  # scale_color_manual(values = c(
  #   "blue2",
  #   "yellow4",
  #   "red4"
  # )) +
  labs(x = "Estimated Change in Vote Choice (0 or 1)")+
  theme(legend.position = "none")

ggsave("Final/Appendix/study3_bynatlorigin.jpg",
  height = 8,
  width = 12
)
```


```{r Study 3 Appendix Descriptives}

names(study3.cleaned.df)

diag_df <- study3.cleaned.df %>%
  group_by(party_id) %>%
  reframe(count=n())

study3.cleaned.df$party_id <- factor(study3.cleaned.df$party_id,
  levels = c(
    "Democrat",
    "Independent lean Dem",
    "Independent no lean",
    "Independent lean Rep",
    "Republican"
  )
)


## Linked fate

study3_lf <- study3.cleaned.df %>%
  group_by(lat.linked.fate,
           party_id) %>%
  reframe(count=n())

study3_lf$`Latino Linked Fate` <- factor(study3_lf$lat.linked.fate,
  levels = c(
    "None at all",
    "A little",
    "A moderate amount",
    "A lot",
    "A great deal"
  )
)


ggplot(
  data = study3_lf,
  aes(
    x = `Latino Linked Fate`,
    y = count,
    fill = `Latino Linked Fate`
  )
) +
  geom_bar(stat = "identity") +
  facet_wrap(~party_id,
    ncol = 3
  ) +
  theme(axis.text.x = element_blank()) +
  labs(
    x = "Response to Latino Linked Fate Question by Respondent Party ID",
    y = "Number of Respondents"
  ) +
  theme(
    legend.position = c(
      0.85,
      0.25
    ),
    legend.title = element_text(size = 18),
    legend.text = element_text(size = 16)
  )

ggsave("Final/Appendix/study3_latlinkedfate_by_partyid.png",
  height = 6,
  width = 10
)



```








